JLayer's view component
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
javax. 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:
Container#add(java.awt.Component) Container#add(String, java.awt.Component) Container#add(java.awt.Component, int) Container#add(java.awt.Component, Object) Container#add(java.awt.Component, Object, int) UnsupportedOperationException to be thrown,
to add a component to JLayer
use setView(Component) or setGlassPane(JPanel).
JLayer(Component), setView(Component), getView(), javax.swing.plaf.LayerUI , JLayer(Component, LayerUI), setUI(javax.swing.plaf.LayerUI) , getUI()
| Modifier and Type | Class and Description |
|---|---|
| private static class | JLayer.
The default glassPane for the |
| private static class | JLayer.
static AWTEventListener to be shared with all AbstractLayerUIs |
| Modifier and Type | Field and Description |
|---|---|
| private static final JLayer. | |
| private long | |
| private JPanel | |
| private transient boolean | |
| private transient boolean | |
| private transient boolean | |
| private LayerUI | |
| private V |
| Access | Constructor and Description |
|---|---|
| public | JLayer()
Creates a new |
| public | JLayer(V
the component to be decorated by this view)JLayerCreates a new |
| public | JLayer(V
the component to be decorated view, LayerUI<V> the ui)javax. delegate
to be used by this JLayerCreates a new |
| Modifier and Type | Method and Description |
|---|---|
| protected void | addImpl(Component comp, Object constraints, int index)
Overrides java. This method is not supported by |
| public void | addNotify()
Overrides javax. Notifies this component that it now has a parent component. |
| public JPanel | Returns: the defaultglassPaneCalled by the constructor methods to create a default |
| public void | doLayout()
Overrides java. Delegates its functionality to the |
| public AccessibleContext | Returns: the AccessibleContext associated with thisJLayer.Overrides java. Implements javax. Gets the AccessibleContext associated with this |
| public Border | Returns: the border object for theview componentOverrides javax. Delegates its functionality to the |
| public JPanel | Returns: theJLayer's glassPane component
or null if none existsReturns the |
| public long | Returns: the bitmask of event types to receive for thisJLayerReturns the bitmap of event mask to receive by this |
| public Dimension | Returns: the preferred size of the viewport for a view componentImplements javax. Returns the preferred size of the viewport for a view component. |
| public int | Returns: the "block" increment for scrolling in the specified directionImplements javax. 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. |
| public boolean | Returns: whether the layer should track the height of the viewportImplements javax. Returns |
| public boolean | Returns: whether the layer should track the width of the viewportImplements javax. Returns |
| public int | Returns: The "unit" increment for scrolling in the specified direction. This value should always be positive.Implements javax. 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. |
| public LayerUI | Returns: theLayerUI for this JLayerOverrides javax. Returns the |
| public V | Returns: theJLayer's view component
or null if none existsReturns the |
| public boolean | Returns: false if the infoflags indicate that the
image is completely loaded; true otherwise.the image being observed img, int see infoflags, int imageUpdate for more informationthe x coordinate x, int the y coordinate y, int the width w, int the height h)Overrides java. Implements java. Delegates its functionality to the
|
| public boolean | Returns: falseOverrides javax. The |
| protected boolean | Returns: trueOverrides javax. Always returns |
| public void | paint(Graphics
the g)Graphics to render toOverrides javax. Delegates all painting to the |
| protected void | paintComponent(Graphics g)
Overrides javax. This method is empty, because all painting is done by
|
| public void | paintImmediately(int
the x value of the region to be painted x, int the y value of the region to be painted y, int the width of the region to be painted w, int the height of the region to be painted h)Overrides javax. Delegates its functionality to the
|
| public void | propertyChange(PropertyChangeEvent evt)
Implements java. This method gets called when a bound property is changed. |
| private void | readObject(ObjectInputStream s)
Hides javax. We use the |
| public void | remove(Component comp)
Overrides java. Removes the specified component from this container. |
| public void | |
| public void | removeNotify()
Overrides javax. Notifies this component that it no longer has a parent component. |
| public void | setBorder(Border
the border to be rendered for the border)view componentOverrides javax. Delegates its functionality to the |
| public void | setGlassPane(JPanel
the glassPane component of this glassPane)JLayerSets the |
| public void | setLayerEventMask(long
the bitmask of event types to receive layerEventMask)Enables the events from JLayer and all its descendants
defined by the specified event mask parameter
to be delivered to the
|
| public void | setLayout(LayoutManager
the specified layout manager mgr)Overrides java. Sets the layout manager for this container. |
| public void | setUI(LayerUI<? super V>
the ui)javax. for this JLayerSets the |
| public void | setView(V
the view component for this view)JLayerSets the |
| public void | updateUI()
Overrides javax. Delegates its functionality to the |
| eventController | back to summary |
|---|---|
| private static final JLayer. | |
| eventMask | back to summary |
|---|---|
| private long eventMask Hides java. | |
| glassPane | back to summary |
|---|---|
| private JPanel glassPane | |
| isImageUpdateCalling | back to summary |
|---|---|
| private transient boolean isImageUpdateCalling | |
| isPaintCalling | back to summary |
|---|---|
| private transient boolean isPaintCalling | |
| isPaintImmediatelyCalling | back to summary |
|---|---|
| private transient boolean isPaintImmediatelyCalling | |
| layerUI | back to summary |
|---|---|
| private LayerUI<? super V> layerUI | |
| view | back to summary |
|---|---|
| private V view | |
| JLayer | back to summary |
|---|---|
| public JLayer() Creates a new | |
| JLayer | back to summary |
|---|---|
| public JLayer(V view) Creates a new
| |
| JLayer | back to summary |
|---|---|
| public JLayer(V view, LayerUI<V> ui) Creates a new
| |
| addImpl | back to summary |
|---|---|
| protected void addImpl(Component comp, Object constraints, int index) Overrides java. This method is not supported by
| |
| addNotify | back to summary |
|---|---|
| public void addNotify() Overrides javax. Doc from javax. Notifies this component that it now has a parent component.
When this method is invoked, the chain of parent components is
set up with | |
| createGlassPane | back to summary |
|---|---|
| public JPanel createGlassPane() Called by the constructor methods to create a default
| |
| doLayout | back to summary |
|---|---|
| public void doLayout() Overrides java. Delegates its functionality to the | |
| getAccessibleContext | back to summary |
|---|---|
| public AccessibleContext getAccessibleContext() Overrides java. Implements javax. Gets the AccessibleContext associated with this
| |
| getBorder | back to summary |
|---|---|
| public Border getBorder() Overrides javax. Delegates its functionality to the
| |
| getGlassPane | back to summary |
|---|---|
| public JPanel getGlassPane() Returns the
| |
| getLayerEventMask | back to summary |
|---|---|
| public long getLayerEventMask() Returns the bitmap of event mask to receive by this
It means that
By default
| |
| getPreferredScrollableViewportSize | back to summary |
|---|---|
| public Dimension getPreferredScrollableViewportSize() Implements javax. Returns the preferred size of the viewport for a view component.
If the view component of this layer implements
| |
| getScrollableBlockIncrement | back to summary |
|---|---|
| public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) Implements javax. 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
| |
| getScrollableTracksViewportHeight | back to summary |
|---|---|
| public boolean getScrollableTracksViewportHeight() Implements javax. Returns
If the view component of this layer implements
| |
| getScrollableTracksViewportWidth | back to summary |
|---|---|
| public boolean getScrollableTracksViewportWidth() Implements javax. Returns
If the view component of this layer implements
| |
| getScrollableUnitIncrement | back to summary |
|---|---|
| public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) Implements javax. 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
If the view component of this layer implements
| |
| getUI | back to summary |
|---|---|
| public LayerUI Overrides javax. Returns the
| |
| getView | back to summary |
|---|---|
| public V getView() Returns the
| |
| imageUpdate | back to summary |
|---|---|
| public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) Overrides java. Implements java. Delegates its functionality to the
| |
| isOptimizedDrawingEnabled | back to summary |
|---|---|
| public boolean isOptimizedDrawingEnabled() Overrides javax. The
| |
| isPaintingOrigin | back to summary |
|---|---|
| protected boolean isPaintingOrigin() Overrides javax. Always returns
| |
| paint | back to summary |
|---|---|
| public void paint(Graphics g) Overrides javax. Delegates all painting to the
| |
| paintComponent | back to summary |
|---|---|
| protected void paintComponent(Graphics g) Overrides javax. This method is empty, because all painting is done by
| |
| paintImmediately | back to summary |
|---|---|
| public void paintImmediately(int x, int y, int w, int h) Overrides javax. Delegates its functionality to the
| |
| propertyChange | back to summary |
|---|---|
| public void propertyChange(PropertyChangeEvent evt) Implements java. Doc from java. This method gets called when a bound property is changed. | |
| readObject | back to summary |
|---|---|
| private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException Hides javax. Doc from javax. We use the
| |
| remove | back to summary |
|---|---|
| public void remove(Component comp) Overrides java. Doc from java. 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
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. | |
| removeAll | back to summary |
|---|---|
| public void removeAll() Overrides java. Doc from java. 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
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. | |
| removeNotify | back to summary |
|---|---|
| public void removeNotify() Overrides javax. Doc from javax. Notifies this component that it no longer has a parent component.
When this method is invoked, any | |
| setBorder | back to summary |
|---|---|
| public void setBorder(Border border) Overrides javax. Delegates its functionality to the
| |
| setGlassPane | back to summary |
|---|---|
| public void setGlassPane(JPanel glassPane) Sets the
| |
| setLayerEventMask | back to summary |
|---|---|
| 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
Events are delivered provided that
The following example shows how to correctly use this method
in the
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.
| |
| setLayout | back to summary |
|---|---|
| public void setLayout(LayoutManager mgr) Overrides java. Sets the layout manager for this container. This method is overridden to prevent the layout manager from being set. Note If
| |
| setUI | back to summary |
|---|---|
| public void setUI(LayerUI<? super V> ui) Sets the
| |
| setView | back to summary |
|---|---|
| public void setView(V view) Sets the
| |
| updateUI | back to summary |
|---|---|
| public void updateUI() Overrides javax. Delegates its functionality to the | |
javax.swing.JLayer .
It is a subclass of JPanel which is non opaque by default.
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: true if this component logically contains x,ythe x coordinate of the point x, int the y coordinate of the point y)Overrides javax. First, implementation of this method iterates through
glassPane's child components and returns |
| DefaultLayerGlassPane | back to summary |
|---|---|
| public DefaultLayerGlassPane() Creates a new | |
| contains | back to summary |
|---|---|
| public boolean contains(int x, int y) Overrides javax. First, implementation of this method iterates through
glassPane's child components and returns
| |
| Modifier and Type | Field and Description |
|---|---|
| private static final long | |
| private long | |
| private ArrayList |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public void | eventDispatched(AWTEvent event)
Implements java. Invoked when an event is dispatched in the AWT. |
| private long | |
| private boolean | |
| private void | |
| private void |
| ACCEPTED_EVENTS | back to summary |
|---|---|
| private static final long ACCEPTED_EVENTS | |
| currentEventMask | back to summary |
|---|---|
| private long currentEventMask | |
| layerMaskList | back to summary |
|---|---|
| private ArrayList<Long> layerMaskList | |
| LayerEventController | back to summary |
|---|---|
| private LayerEventController() | |
| addAWTEventListener | back to summary |
|---|---|
| private void addAWTEventListener(final long eventMask) | |
| eventDispatched | back to summary |
|---|---|
| public void eventDispatched(AWTEvent event) Implements java. Doc from java. Invoked when an event is dispatched in the AWT.
| |
| getCurrentEventMask | back to summary |
|---|---|
| private long getCurrentEventMask() | |
| isEventEnabled | back to summary |
|---|---|
| private boolean isEventEnabled(long eventMask, int id) | |
| removeAWTEventListener | back to summary |
|---|---|
| private void removeAWTEventListener() | |
| updateAWTEventListener | back to summary |
|---|---|
| private void updateAWTEventListener(long oldEventMask, long newEventMask) | |