Package Summary  Overview Summary

class:JLayer [NONE]

Type Parameters:
V - the type of JLayer's view component
All Implemented Interfaces:
ImageObserver, MenuContainer, PropertyChangeListener, Serializable, EventListener, Accessible, Scrollable

public final class JLayer<V extends Component>
extends JComponent
implements Scrollable, PropertyChangeListener, Accessible
JLayer is a universal decorator for Swing components which enables you to implement various advanced painting effects as well as receive notifications of all AWTEvents generated within its borders.

JLayer delegates the handling of painting and input events to a LayerUI object, which performs the actual decoration.

The custom painting implemented in the LayerUI and events notification work for the JLayer itself and all its subcomponents. This combination enables you to enrich existing components by adding new advanced functionality such as temporary locking of a hierarchy, data tips for compound components, enhanced mouse scrolling etc and so on.

JLayer is a good solution if you only need to do custom painting over compound component or catch input events from its subcomponents.

 import javax.swing.*;
 import javax.swing.plaf.LayerUI;
 import java.awt.*;

 public class JLayerSample {

     private static JLayer<JComponent> createLayer() {
         // This custom layerUI will fill the layer with translucent green
         // and print out all mouseMotion events generated within its borders
         LayerUI<JComponent> layerUI = new LayerUI<JComponent>() {

             public void paint(Graphics g, JComponent c) {
                 // paint the layer as is
                 super.paint(g, c);
                 // fill it with the translucent green
                 g.setColor(new Color(0, 128, 0, 128));
                 g.fillRect(0, 0, c.getWidth(), c.getHeight());
             }

             public void installUI(JComponent c) {
                 super.installUI(c);
                 // enable mouse motion events for the layer's subcomponents
                 ((JLayer) c).setLayerEventMask(AWTEvent.MOUSE_MOTION_EVENT_MASK);
             }

             public void uninstallUI(JComponent c) {
                 super.uninstallUI(c);
                 // reset the layer event mask
                 ((JLayer) c).setLayerEventMask(0);
             }

             // overridden method which catches MouseMotion events
             public void eventDispatched(AWTEvent e, JLayer<? extends JComponent> l) {
                 System.out.println("AWTEvent detected: " + e);
             }
         };
         // create a component to be decorated with the layer
         JPanel panel = new JPanel();
         panel.add(new JButton("JButton"));

         // create the layer for the panel using our custom layerUI
         return new JLayer<JComponent>(panel, layerUI);
     }

     private static void createAndShowGUI() {
         final JFrame frame = new JFrame();
         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

         // work with the layer as with any other Swing component
         frame.add(createLayer());

         frame.setSize(200, 200);
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);
     }

     public static void main(String[] args) throws Exception {
         SwingUtilities.invokeAndWait(new Runnable() {
             public void run() {
                 createAndShowGUI();
             }
         });
     }
 }
 
Note:JLayer doesn't support the following methods: using any of them will cause UnsupportedOperationException to be thrown, to add a component to JLayer use setView(Component) or setGlassPane(JPanel).

Since:
1.7
See Also:
JLayer(Component), setView(Component), getView(), LayerUI, JLayer(Component, LayerUI) , setUI(javax.swing.plaf.LayerUI), getUI()

constructor:JLayer() [NONE]

method:getView() [NONE]

  • getView

    public V getView()
    Returns the JLayer's view component or null.
    This is a bound property.
    Returns:
    the JLayer's view component or null if none exists
    See Also:
    setView(Component)
  • setView

    public void setView?(V view)
    Sets the JLayer's view component, which can be null.
    This is a bound property.
    Parameters:
    view - the view component for this JLayer
    See Also:
    getView()
  • method:setUI(javax.swing.plaf.LayerUI) [NONE]

    setUI

    public void setUI?(LayerUI<? super V> ui)
    Sets the LayerUI which will perform painting and receive input events for this JLayer.
    Parameters:
    ui - the LayerUI for this JLayer

    method:getUI() [NONE]

    getUI

    public LayerUI<? super V>  getUI()
    Returns the LayerUI for this JLayer.
    Overrides:
    getUI in class JComponent
    Returns:
    the LayerUI for this JLayer

    method:getGlassPane() [NONE]

    getGlassPane

    public JPanel getGlassPane()
    Returns the JLayer's glassPane component or null.
    This is a bound property.
    Returns:
    the JLayer's glassPane component or null if none exists
    See Also:
    setGlassPane(JPanel)

    method:setGlassPane(javax.swing.JPanel) [NONE]

    setGlassPane

    public void setGlassPane?(JPanel glassPane)
    Sets the JLayer's glassPane component, which can be null.
    This is a bound property.
    Parameters:
    glassPane - the glassPane component of this JLayer
    See Also:
    getGlassPane()

    method:createGlassPane() [NONE]

    createGlassPane

    public JPanel createGlassPane()
    Called by the constructor methods to create a default glassPane. By default this method creates a new JPanel with visibility set to true and opacity set to false.
    Returns:
    the default glassPane

    method:setLayout(java.awt.LayoutManager) [NONE]

    setLayout

    public void setLayout?(LayoutManager mgr)
    Sets the layout manager for this container. This method is overridden to prevent the layout manager from being set.

    Note: If mgr is non-null, this method will throw an exception as layout managers are not supported on a JLayer.

    Overrides:
    setLayout in class Container
    Parameters:
    mgr - the specified layout manager
    Throws:
    IllegalArgumentException - this method is not supported
    See Also:
    Container.doLayout(), Container.getLayout(), Container.invalidate()

    method:setBorder(javax.swing.border.Border) [NONE]

    setBorder

    public void setBorder?(Border border)
    Delegates its functionality to the getView().setBorder(Border) method, if the view component is an instance of javax.swing.JComponent, otherwise this method is a no-op.
    Overrides:
    setBorder in class JComponent
    Parameters:
    border - the border to be rendered for the view component
    See Also:
    getView(), JComponent.setBorder(Border)

    method:getBorder() [NONE]

    getBorder

    public Border getBorder()
    Delegates its functionality to the getView().getBorder() method, if the view component is an instance of javax.swing.JComponent, otherwise returns null.
    Overrides:
    getBorder in class JComponent
    Returns:
    the border object for the view component
    See Also:
    getView(), setBorder(javax.swing.border.Border), JComponent.getBorder()

    method:addImpl(java.awt.Component,java.lang.Object,int) [NONE]

    addImpl

    protected void addImpl?(Component comp, Object constraints, int index)
    This method is not supported by JLayer and always throws UnsupportedOperationException
    Overrides:
    addImpl in class Container
    Parameters:
    comp - the component to be added
    constraints - an object expressing layout constraints for this component
    index - the position in the container's list at which to insert the component, where -1 means append to the end
    Throws:
    UnsupportedOperationException - this method is not supported
    See Also:
    setView(Component), setGlassPane(JPanel)

    method:remove(java.awt.Component) [NONE]

    remove

    public void remove?(Component comp)
    Removes the specified component from this container. This method also notifies the layout manager to remove the component from this container's layout via the removeLayoutComponent method.

    This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to reflect the changes.

    Overrides:
    remove in class Container
    Parameters:
    comp - the component to be removed
    See Also:
    Container.add(java.awt.Component), Container.invalidate(), Container.validate(), Container.remove(int)

    method:removeAll() [NONE]

    removeAll

    public void removeAll()
    Removes all the components from this container. This method also notifies the layout manager to remove the components from this container's layout via the removeLayoutComponent method.

    This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to reflect the changes.

    Overrides:
    removeAll in class Container
    See Also:
    Container.add(java.awt.Component), Container.remove(int), Container.invalidate()

    method:isPaintingOrigin() [NONE]

    isPaintingOrigin

    protected boolean isPaintingOrigin()
    Always returns true to cause painting to originate from JLayer, or one of its ancestors.
    Overrides:
    isPaintingOrigin in class JComponent
    Returns:
    true
    See Also:
    JComponent.isPaintingOrigin()

    method:paintImmediately(int,int,int,int) [NONE]

    paintImmediately

    public void paintImmediately?(int x, int y, int w, int h)
    Delegates its functionality to the LayerUI.paintImmediately(int, int, int, int, JLayer) method, if LayerUI is set.
    Overrides:
    paintImmediately in class JComponent
    Parameters:
    x - the x value of the region to be painted
    y - the y value of the region to be painted
    w - the width of the region to be painted
    h - the height of the region to be painted
    See Also:
    JComponent.repaint(long, int, int, int, int) , JComponent.isPaintingOrigin()

    method:imageUpdate(java.awt.Image,int,int,int,int,int) [NONE]

    imageUpdate

    public boolean imageUpdate?(Image img, int infoflags, int x, int y, int w, int h)
    Delegates its functionality to the LayerUI.imageUpdate(java.awt.Image, int, int, int, int, int, JLayer) method, if the LayerUI is set.
    Specified by:
    imageUpdate in interface ImageObserver
    Overrides:
    imageUpdate in class Component
    Parameters:
    img - the image being observed
    infoflags - see imageUpdate for more information
    x - the x coordinate
    y - the y coordinate
    w - the width
    h - the height
    Returns:
    false if the infoflags indicate that the image is completely loaded; true otherwise.
    See Also:
    ImageObserver, Graphics.drawImage(Image, int, int, Color, java.awt.image.ImageObserver) , Graphics.drawImage(Image, int, int, java.awt.image.ImageObserver) , Graphics.drawImage(Image, int, int, int, int, Color, java.awt.image.ImageObserver) , Graphics.drawImage(Image, int, int, int, int, java.awt.image.ImageObserver) , ImageObserver.imageUpdate(java.awt.Image, int, int, int, int, int)

    method:paint(java.awt.Graphics) [NONE]

    paint

    public void paint?(Graphics g)
    Delegates all painting to the LayerUI object.
    Overrides:
    paint in class JComponent
    Parameters:
    g - the Graphics to render to
    See Also:
    JComponent.paintComponent(java.awt.Graphics), JComponent.paintBorder(java.awt.Graphics), JComponent.paintChildren(java.awt.Graphics), JComponent.getComponentGraphics(java.awt.Graphics), JComponent.repaint(long, int, int, int, int)

    method:paintComponent(java.awt.Graphics) [NONE]

    paintComponent

    protected void paintComponent?(Graphics g)
    This method is empty, because all painting is done by paint(Graphics) and ComponentUI.update(Graphics, JComponent) methods
    Overrides:
    paintComponent in class JComponent
    Parameters:
    g - the Graphics object to protect
    See Also:
    JComponent.paint(java.awt.Graphics), ComponentUI

    method:isOptimizedDrawingEnabled() [NONE]

    isOptimizedDrawingEnabled

    public boolean isOptimizedDrawingEnabled()
    The JLayer overrides the default implementation of this method (in JComponent) to return false. This ensures that the drawing machinery will call the JLayer's paint implementation rather than messaging the JLayer's children directly.
    Overrides:
    isOptimizedDrawingEnabled in class JComponent
    Returns:
    false

    method:propertyChange(java.beans.PropertyChangeEvent) [NONE]

    propertyChange

    public void propertyChange?(PropertyChangeEvent evt)
    This method gets called when a bound property is changed.
    Specified by:
    propertyChange in interface PropertyChangeListener
    Parameters:
    evt - A PropertyChangeEvent object describing the event source and the property that has changed.

    method:setLayerEventMask(long) [NONE]

    setLayerEventMask

    public void setLayerEventMask?(long layerEventMask)
    Enables the events from JLayer and all its descendants defined by the specified event mask parameter to be delivered to the LayerUI.eventDispatched(AWTEvent, JLayer) method.

    Events are delivered provided that LayerUI is set for this JLayer and the JLayer is displayable.

    The following example shows how to correctly use this method in the LayerUI implementations:

        public void installUI(JComponent c) {
           super.installUI(c);
           JLayer l = (JLayer) c;
           // this LayerUI will receive only key and focus events
           l.setLayerEventMask(AWTEvent.KEY_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
        }
    
        public void uninstallUI(JComponent c) {
           super.uninstallUI(c);
           JLayer l = (JLayer) c;
           // JLayer must be returned to its initial state
           l.setLayerEventMask(0);
        }
     
    By default JLayer receives no events and its event mask is 0.

    Parameters:
    layerEventMask - the bitmask of event types to receive
    See Also:
    getLayerEventMask(), LayerUI.eventDispatched(AWTEvent, JLayer) , Component.isDisplayable()

    method:getLayerEventMask() [NONE]

    getLayerEventMask

    public long getLayerEventMask()
    Returns the bitmap of event mask to receive by this JLayer and its LayerUI.

    It means that LayerUI.eventDispatched(AWTEvent, JLayer) method will only receive events that match the event mask.

    By default JLayer receives no events.

    Returns:
    the bitmask of event types to receive for this JLayer

    method:updateUI() [NONE]

    updateUI

    public void updateUI()
    Delegates its functionality to the LayerUI.updateUI(JLayer) method, if LayerUI is set.
    Overrides:
    updateUI in class JComponent
    See Also:
    JComponent.setUI(javax.swing.plaf.ComponentUI), UIManager.getLookAndFeel(), UIManager.getUI(javax.swing.JComponent)

    method:getPreferredScrollableViewportSize() [NONE]

    getPreferredScrollableViewportSize

    public Dimension getPreferredScrollableViewportSize()
    Returns the preferred size of the viewport for a view component.

    If the view component of this layer implements Scrollable, this method delegates its implementation to the view component.

    Specified by:
    getPreferredScrollableViewportSize in interface Scrollable
    Returns:
    the preferred size of the viewport for a view component
    See Also:
    Scrollable

    method:getScrollableBlockIncrement(java.awt.Rectangle,int,int) [NONE]

    getScrollableBlockIncrement

    public int getScrollableBlockIncrement?(Rectangle visibleRect, int orientation, int direction)
    Returns a scroll increment, which is required for components that display logical rows or columns in order to completely expose one block of rows or columns, depending on the value of orientation.

    If the view component of this layer implements Scrollable, this method delegates its implementation to the view component.

    Specified by:
    getScrollableBlockIncrement in interface Scrollable
    Parameters:
    visibleRect - The view area visible within the viewport
    orientation - Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
    direction - Less than zero to scroll up/left, greater than zero for down/right.
    Returns:
    the "block" increment for scrolling in the specified direction
    See Also:
    Scrollable

    method:getScrollableTracksViewportHeight() [NONE]

    getScrollableTracksViewportHeight

    public boolean getScrollableTracksViewportHeight()
    Returns false to indicate that the height of the viewport does not determine the height of the layer, unless the preferred height of the layer is smaller than the height of the viewport.

    If the view component of this layer implements Scrollable, this method delegates its implementation to the view component.

    Specified by:
    getScrollableTracksViewportHeight in interface Scrollable
    Returns:
    whether the layer should track the height of the viewport
    See Also:
    Scrollable

    method:getScrollableTracksViewportWidth() [NONE]

    getScrollableTracksViewportWidth

    public boolean getScrollableTracksViewportWidth()
    Returns false to indicate that the width of the viewport does not determine the width of the layer, unless the preferred width of the layer is smaller than the width of the viewport.

    If the view component of this layer implements Scrollable, this method delegates its implementation to the view component.

    Specified by:
    getScrollableTracksViewportWidth in interface Scrollable
    Returns:
    whether the layer should track the width of the viewport
    See Also:
    Scrollable

    method:getScrollableUnitIncrement(java.awt.Rectangle,int,int) [NONE]

    getScrollableUnitIncrement

    public int getScrollableUnitIncrement?(Rectangle visibleRect, int orientation, int direction)
    Returns a scroll increment, which is required for components that display logical rows or columns in order to completely expose one new row or column, depending on the value of orientation. Ideally, components should handle a partially exposed row or column by returning the distance required to completely expose the item.

    Scrolling containers, like JScrollPane, will use this method each time the user requests a unit scroll.

    If the view component of this layer implements Scrollable, this method delegates its implementation to the view component.

    Specified by:
    getScrollableUnitIncrement in interface Scrollable
    Parameters:
    visibleRect - The view area visible within the viewport
    orientation - Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
    direction - Less than zero to scroll up/left, greater than zero for down/right.
    Returns:
    The "unit" increment for scrolling in the specified direction. This value should always be positive.
    See Also:
    Scrollable

    method:addNotify() [NONE]

    addNotify

    public void addNotify()
    Notifies this component that it now has a parent component. When this method is invoked, the chain of parent components is set up with KeyboardAction event listeners. This method is called by the toolkit internally and should not be called directly by programs.
    Overrides:
    addNotify in class JComponent
    See Also:
    JComponent.registerKeyboardAction(java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int)

    method:removeNotify() [NONE]

    removeNotify

    public void removeNotify()
    Notifies this component that it no longer has a parent component. When this method is invoked, any KeyboardActions set up in the chain of parent components are removed. This method is called by the toolkit internally and should not be called directly by programs.
    Overrides:
    removeNotify in class JComponent
    See Also:
    JComponent.registerKeyboardAction(java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int)

    method:doLayout() [NONE]

    doLayout

    public void doLayout()
    Delegates its functionality to the LayerUI.doLayout(JLayer) method, if LayerUI is set.
    Overrides:
    doLayout in class Container
    See Also:
    LayoutManager.layoutContainer(java.awt.Container), Container.setLayout(java.awt.LayoutManager), Container.validate()

    method:getAccessibleContext() [NONE]

    getAccessibleContext

    public AccessibleContext getAccessibleContext()
    Gets the AccessibleContext associated with this JLayer.
    Specified by:
    getAccessibleContext in interface Accessible
    Overrides:
    getAccessibleContext in class Component
    Returns:
    the AccessibleContext associated with this JLayer.

    © 2020 Oracle Corporation and/or its affiliates