Class FadeLayerUI

java.lang.Object
javax.swing.plaf.ComponentUI
javax.swing.plaf.LayerUI<JPanel>
ca.corbett.extras.image.animation.FadeLayerUI
All Implemented Interfaces:
Serializable

public class FadeLayerUI extends LayerUI<JPanel>
This is a LayerUI implementation that can be used to "fade" a JPanel in and out by overlaying a colored translucent layer that animates its opacity. This can be a neat way of providing a transition effect if you need to swap out the content being shown in a JPanel, instead of just abruptly changing it.

USAGE: Create a JLayer with an instance of this FadeLayerUI, wrapping the JPanel you want to fade in/out. Call fadeOut() to start the fade out animation, and provide a Runnable that will be executed when the fade out completes (typically to swap the content being shown). Then call fadeIn() to fade back in, again providing a Runnable to be executed when the fade in completes (typically to re-enable user interaction).

     JPanel contentPanel = ...;
     FadeLayerUI fadeLayerUI = new FadeLayerUI();
     JLayer<JPanel> layeredPanel = new JLayer<>(contentPanel, fadeLayerUI);
     containerPanel.add(layeredPanel);

     // Set your fade options, or live with the defaults:
     fadeLayerUI.setFadeColor(Color.BLUE);
     fadeLayerUI.setAnimationDuration(FadeLayerUI.AnimationDuration.VeryLong);

     // Then, in a button handler or whatever:
     fadeLayerUI.fadeOut(() -> {
         // Swap content here
         fadeLayerUI.fadeIn(() ->
             // Re-enable interaction here
         });
     });

     // Or, more simply, if you just want to fade out, swap, and fade in:
     fadeLayerUI.fadeOut(() -> {
         // Swap content here
         fadeLayerUI.fadeIn(null); // No action needed after fade in
     });
 
Since:
swing-extras 2.7
Author:
scorbo2 with claude.ai
See Also:
  • Field Details

    • DEFAULT_FADE_COLOR

      public static final Color DEFAULT_FADE_COLOR
    • DEFAULT_ANIMATION_DURATION

      public static final AnimationDuration DEFAULT_ANIMATION_DURATION
    • DEFAULT_ANIMATION_SPEED

      public static final AnimationSpeed DEFAULT_ANIMATION_SPEED
  • Constructor Details

    • FadeLayerUI

      public FadeLayerUI()
      Creates a new FadeLayerUI with default settings. You can associate it with a JLayer<JPanel> to use it:
           JPanel contentPanel = ...;
           FadeLayerUI fadeLayerUI = new FadeLayerUI();
           JLayer<JPanel> layeredPanel = new JLayer<>(contentPanel, fadeLayerUI);
       

      Then you can use the fadeOut() and fadeIn() methods in this class to perform the fade animations.

  • Method Details

    • getFadeColor

      public Color getFadeColor()
      Gets the fade color that will be used during the fade animation.
    • setFadeColor

      public FadeLayerUI setFadeColor(Color fadeColor)
      Sets the fade color that will be used during the fade animation. The default is white.
    • getAnimationDuration

      public AnimationDuration getAnimationDuration()
      Gets the configured animation duration.
    • setAnimationDuration

      public FadeLayerUI setAnimationDuration(AnimationDuration animationDuration)
      Sets the animation duration.
    • getAnimationSpeed

      public AnimationSpeed getAnimationSpeed()
      Gets the configured animation speed.
    • setAnimationSpeed

      public FadeLayerUI setAnimationSpeed(AnimationSpeed animationSpeed)
      Sets the animation speed.
    • fadeOut

      public void fadeOut(Runnable onComplete)
      Performs a "fade out" animation, fading the content to the configured fade color. Optionally, you can provide a Runnable that will be executed when the fade out completes. This is typically where you would swap the content being shown in the underlying JPanel.
      Parameters:
      onComplete - An optional Runnable to invoke when the fade out completes (may be null).
    • fadeIn

      public void fadeIn(Runnable onComplete)
      Performs a "fade in" animation, fading the content back from the fade color to fully visible. Optionally, you can provide a Runnable that will be executed when the fade in completes. This is typically where you would re-enable user interaction with the underlying JPanel.
      Parameters:
      onComplete - An optional Runnable to invoke when the fade in completes (may be null).
    • installUI

      public void installUI(JComponent c)
      Overrides:
      installUI in class LayerUI<JPanel>
    • uninstallUI

      public void uninstallUI(JComponent c)
      Overrides:
      uninstallUI in class LayerUI<JPanel>
    • paint

      public void paint(Graphics g, JComponent c)
      Overrides:
      paint in class LayerUI<JPanel>
    • processMouseEvent

      protected void processMouseEvent(MouseEvent e, JLayer<? extends JPanel> l)
      Overrides:
      processMouseEvent in class LayerUI<JPanel>
    • processMouseMotionEvent

      protected void processMouseMotionEvent(MouseEvent e, JLayer<? extends JPanel> l)
      Overrides:
      processMouseMotionEvent in class LayerUI<JPanel>
    • processKeyEvent

      protected void processKeyEvent(KeyEvent e, JLayer<? extends JPanel> l)
      Overrides:
      processKeyEvent in class LayerUI<JPanel>