Class FadeLayerUI
- All Implemented Interfaces:
Serializable
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final AnimationDurationstatic final AnimationSpeedstatic final Color -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidPerforms a "fade in" animation, fading the content back from the fade color to fully visible.voidPerforms a "fade out" animation, fading the content to the configured fade color.Gets the configured animation duration.Gets the configured animation speed.Gets the fade color that will be used during the fade animation.voidvoidpaint(Graphics g, JComponent c) protected voidprocessKeyEvent(KeyEvent e, JLayer<? extends JPanel> l) protected voidprocessMouseEvent(MouseEvent e, JLayer<? extends JPanel> l) protected voidprocessMouseMotionEvent(MouseEvent e, JLayer<? extends JPanel> l) setAnimationDuration(AnimationDuration animationDuration) Sets the animation duration.setAnimationSpeed(AnimationSpeed animationSpeed) Sets the animation speed.setFadeColor(Color fadeColor) Sets the fade color that will be used during the fade animation.voidMethods inherited from class javax.swing.plaf.LayerUI
addPropertyChangeListener, addPropertyChangeListener, applyPropertyChange, doLayout, eventDispatched, firePropertyChange, getBaseline, getBaselineResizeBehavior, getMaximumSize, getMinimumSize, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, imageUpdate, paintImmediately, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, removePropertyChangeListener, removePropertyChangeListener, updateUIMethods inherited from class javax.swing.plaf.ComponentUI
contains, createUI, getAccessibleChild, getAccessibleChildrenCount, update
-
Field Details
-
DEFAULT_FADE_COLOR
-
DEFAULT_ANIMATION_DURATION
-
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
Gets the fade color that will be used during the fade animation. -
setFadeColor
Sets the fade color that will be used during the fade animation. The default is white. -
getAnimationDuration
Gets the configured animation duration. -
setAnimationDuration
Sets the animation duration. -
getAnimationSpeed
Gets the configured animation speed. -
setAnimationSpeed
Sets the animation speed. -
fadeOut
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
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
-
uninstallUI
- Overrides:
uninstallUIin classLayerUI<JPanel>
-
paint
-
processMouseEvent
- Overrides:
processMouseEventin classLayerUI<JPanel>
-
processMouseMotionEvent
- Overrides:
processMouseMotionEventin classLayerUI<JPanel>
-
processKeyEvent
- Overrides:
processKeyEventin classLayerUI<JPanel>
-