Class JGlassPane

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, StylableComponent

public class JGlassPane extends JPanel implements StylableComponent
A more advanced glass pane implementation than the default Swing glass pane of a JRootPane object (A regular JPanel. In contrast to the default glass pane (JPanel) of a JRootPane, this pane handles any mouse events, without interrupting the controls underneath the glass pane (in the content pane of the root pane). Also, cursors are handled as if the glass pane was invisible (if no cursor gets explicitly set to the glass pane).

This glass pane is also the backbone of how SwingTree renders its drag and drop operations. It renders an illustration of what is being dragged on top of the entire component hierarchy of the root pane.
Through this class SwingTree also draws the overlays for the GUI debug dev tool. The debug dev tool can be enabled by calling SwingTree.setDevToolEnabled(boolean) or more commonly by pressing the shortcut Ctrl+Shift+I (similar to the shortcut for the dev tools in web browsers).


The Component.isVisible() flag of this glass pane is self-managed based on the existence of active named paint jobs registered through the setPaintJobWithId(String, Painter) method. So if there is at least one named paint job registered, then the glass pane will be visible, and if there are no active named paint jobs, then the glass pane will be invisible. Paint jobs can be removed by their id through the removePaintJobWithId(String) method.
Both the drag and drop visualization and the GUI debug dev tool overlays are implemented as named paint jobs on this glass pane, so they will automatically show and hide the glass pane when they are active or inactive.

Note that both paintComponent(Graphics) and paintChildren(Graphics) methods are final and cannot be overridden by subclasses of this glass pane. To do custom painting on this glass pane, you must do so through the setPaintJobWithId(String, Painter) method.
This is important to ensure that existing functionality can not break!

See Also:
  • Field Details

    • rootPane

      protected @Nullable JRootPane rootPane
  • Constructor Details

    • JGlassPane

      public JGlassPane()
      Creates a new JGlassPane with a default MigLayout instance configured to be without insets and gaps between components.
    • JGlassPane

      public JGlassPane(JRootPane rootPane)
  • Method Details

    • setPaintJobWithId

      protected final void setPaintJobWithId(String id, Painter painter)
      Schedules a permanent paint job on this glass pane with the given name/id and painter, which will be active until the removePaintJobWithId(String) method is called with the same id. If there is already an active paint job with the same id, then this method does nothing. Note that paint jobs are executed in the order they were added...
      Parameters:
      id - The id of the paint job. It must be unique, otherwise the paint job will not be added.
      painter - The painter that will be executed from this glass pane's paintChildren method while the paint job is active.
    • removePaintJobWithId

      protected final void removePaintJobWithId(String id)
      Removes the active paint job with the given id from this glass pane, if it exists. If there is no active paint job with the given id, then this method does nothing. Note that paint jobs are executed in the order they were added...
      Parameters:
      id - The id of the paint job to be removed.
    • paintComponent

      public final void paintComponent(Graphics g)
      Overrides:
      paintComponent in class JComponent
    • paintChildren

      public final void paintChildren(Graphics g)
      This method is expected to be implemented as follows:
            @Override
            public void paintChildren(Graphics g){
                paintForeground(g, ()->super.paintChildren(g));
            }
        
      Specified by:
      paintChildren in interface StylableComponent
      Overrides:
      paintChildren in class JComponent
      Parameters:
      g - the graphics context to paint on, obtained from the component's JComponent::paintChildren(Graphics) method.
    • setUISilently

      public void setUISilently(ComponentUI ui)
      Description copied from interface: StylableComponent
      Certain style configurations require SwingTree to install a custom UI delegate. This method is used to set the UI delegate for the component but without triggering side effects like the former UI being uninstalled (which itself can cause a lot of undesired side effects).

      This method is not intended to be called by client code! It exists for internal use only and unfortunately cannot be protected or private due to the nature of the Swing API.

      The implementation of this method is expected to look like this:

        @Override
        public void setUISilently(ComponentUI ui){
            this.ui = ui; // no side effects
        }
        
      Specified by:
      setUISilently in interface StylableComponent
      Parameters:
      ui - the UI delegate to set for the component without triggering side effects.
    • updateUI

      public void updateUI()
      Resets the UI property with a value from the current look and feel.
      Overrides:
      updateUI in class JPanel
      See Also:
    • attachToRootPane

      protected void attachToRootPane(JRootPane rootPane)
      Marries this glass pane to the supplied JRootPane object and detaches it from the previous root pane using the detachFromRootPane(JRootPane) method, if a previous root pane was attached.
      Parameters:
      rootPane - The JRootPane object to which this glass pane should be attached.
    • detachFromRootPane

      protected void detachFromRootPane(@Nullable JRootPane rootPane)
      Detaches this glass pane from the supplied JRootPane object, if the JRootPane.getGlassPane() method returns this glass pane.
      Parameters:
      rootPane - The JRootPane object from which this glass pane should be detached.
    • toRootPane

      public void toRootPane(@Nullable JRootPane pane)
      Marries this glass pane to a JRootPane object. Note that it is expected that the supplied JRootPane is the same root pane that this current glass pane is attached to. If the supplied JRootPane is null, then this glass pane is detached from the current root pane.
      Parameters:
      pane - The JRootPane object to which this glass pane should be attached.
    • getMouseListeners

      public final MouseListener[] getMouseListeners()
      Overrides:
      getMouseListeners in class Component
    • addMouseListener

      public final void addMouseListener(MouseListener listener)
      Overrides:
      addMouseListener in class Component
    • removeMouseListener

      public final void removeMouseListener(MouseListener listener)
      Overrides:
      removeMouseListener in class Component
    • getMouseMotionListeners

      public final MouseMotionListener[] getMouseMotionListeners()
      Overrides:
      getMouseMotionListeners in class Component
    • addMouseMotionListener

      public final void addMouseMotionListener(MouseMotionListener listener)
      Overrides:
      addMouseMotionListener in class Component
    • removeMouseMotionListener

      public final void removeMouseMotionListener(MouseMotionListener listener)
      Overrides:
      removeMouseMotionListener in class Component
    • getMouseWheelListeners

      public final MouseWheelListener[] getMouseWheelListeners()
      Overrides:
      getMouseWheelListeners in class Component
    • addMouseWheelListener

      public final void addMouseWheelListener(MouseWheelListener listener)
      Overrides:
      addMouseWheelListener in class Component
    • removeMouseWheelListener

      public final void removeMouseWheelListener(MouseWheelListener listener)
      Overrides:
      removeMouseWheelListener in class Component
    • contains

      public boolean contains(int x, int y)
      If someone sets a new cursor to the GlassPane we expect that they know what they are doing and return the super.contains(x,y) otherwise we return false to respect the cursors for the underneath components
      Overrides:
      contains in class JComponent