V
- the type of JLayer
's view componentpublic 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 AWTEvent
s 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(JFrame.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()
,
LayerUI
,
JLayer(Component, LayerUI)
,
setUI(javax.swing.plaf.LayerUI)
,
getUI()
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
JLayer()
|
JLayer(V view)
Creates a new
JLayer object
with default LayerUI . |
JLayer(V view,
LayerUI<V> ui)
Creates a new
JLayer object with the specified view component
and LayerUI object. |
Modifier and Type | Method and Description |
---|---|
protected void |
addImpl(Component comp,
Object constraints,
int index)
This method is not supported by
JLayer
and always throws UnsupportedOperationException |
void |
addNotify()
Notifies this component that it now has a parent component.
|
JPanel |
createGlassPane()
Called by the constructor methods to create a default
glassPane . |
void |
doLayout()
Delegates its functionality to the
LayerUI.doLayout(JLayer) method,
if LayerUI is set. |
AccessibleContext |
getAccessibleContext()
Gets the AccessibleContext associated with this
JLayer . |
JPanel |
getGlassPane()
Returns the
JLayer 's glassPane component or null . |
long |
getLayerEventMask()
Returns the bitmap of event mask to receive by this
JLayer
and its LayerUI . |
Dimension |
getPreferredScrollableViewportSize()
Returns the preferred size of the viewport for a view component.
|
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.
|
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. |
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. |
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.
|
LayerUI<? super V> |
getUI()
Returns the
LayerUI for this JLayer . |
V |
getView()
Returns the
JLayer 's view component or null . |
boolean |
isOptimizedDrawingEnabled()
The
JLayer overrides the default implementation of
this method (in JComponent ) to return false . |
protected boolean |
isPaintingOrigin()
Always returns
true to cause painting to originate from JLayer ,
or one of its ancestors. |
void |
paint(Graphics g)
Delegates all painting to the
LayerUI object. |
protected void |
paintComponent(Graphics g)
This method is empty, because all painting is done by
paint(Graphics) and
ComponentUI.update(Graphics, JComponent) methods |
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. |
void |
propertyChange(PropertyChangeEvent evt)
This method gets called when a bound property is changed.
|
void |
remove(Component comp)
Removes the specified component from this container.
|
void |
removeAll()
Removes all the components from this container.
|
void |
removeNotify()
Notifies this component that it no longer has a parent component.
|
void |
setBorder(Border border)
A non-
null border, or non-zero insets, isn't supported, to prevent the geometry
of this component from becoming complex enough to inhibit
subclassing of LayerUI class. |
void |
setGlassPane(JPanel glassPane)
Sets the
JLayer 's glassPane component, which can be null . |
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. |
void |
setLayout(LayoutManager mgr)
Sets the layout manager for this container.
|
void |
setUI(LayerUI<? super V> ui)
Sets the
LayerUI which will perform painting
and receive input events for this JLayer . |
void |
setView(V view)
Sets the
JLayer 's view component, which can be null . |
void |
updateUI()
Delegates its functionality to the
LayerUI.updateUI(JLayer) method,
if LayerUI is set. |
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public JLayer()
public JLayer(V view)
JLayer
object
with default LayerUI
.view
- the component to be decorated by this JLayer
setUI(javax.swing.plaf.LayerUI<? super V>)
public V getView()
JLayer
's view component or null
.
JLayer
's view component
or null
if none existssetView(Component)
public void setView(V view)
JLayer
's view component, which can be null
.
view
- the view component for this JLayer
getView()
public void setUI(LayerUI<? super V> ui)
LayerUI
which will perform painting
and receive input events for this JLayer
.ui
- the LayerUI
for this JLayer
public LayerUI<? super V> getUI()
LayerUI
for this JLayer
.LayerUI
for this JLayer
public JPanel getGlassPane()
JLayer
's glassPane component or null
.
JLayer
's glassPane component
or null
if none existssetGlassPane(JPanel)
public void setGlassPane(JPanel glassPane)
JLayer
's glassPane component, which can be null
.
glassPane
- the glassPane component of this JLayer
getGlassPane()
public JPanel createGlassPane()
glassPane
.
By default this method creates a new JPanel with visibility set to true
and opacity set to false.glassPane
public void setLayout(LayoutManager mgr)
mgr
is non-null
, this
method will throw an exception as layout managers are not supported on
a JLayer
.setLayout
in class Container
mgr
- the specified layout managerIllegalArgumentException
- this method is not supportedContainer.doLayout()
,
Container.getLayout()
,
Container.invalidate()
public void setBorder(Border border)
null
border, or non-zero insets, isn't supported, to prevent the geometry
of this component from becoming complex enough to inhibit
subclassing of LayerUI
class. To create a JLayer
with a border,
add it to a JPanel
that has a border.
Note: If border
is non-null
, this
method will throw an exception as borders are not supported on
a JLayer
.setBorder
in class JComponent
border
- the Border
to setIllegalArgumentException
- this method is not supportedBorder
,
CompoundBorder
protected void addImpl(Component comp, Object constraints, int index)
JLayer
and always throws UnsupportedOperationException
addImpl
in class Container
comp
- the component to be addedconstraints
- an object expressing layout constraints
for this componentindex
- the position in the container's list at which to
insert the component, where -1
means append to the endUnsupportedOperationException
- this method is not supportedsetView(Component)
,
setGlassPane(JPanel)
public void remove(Component comp)
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.
remove
in class Container
comp
- the component to be removedContainer.add(java.awt.Component)
,
Container.invalidate()
,
Container.validate()
,
Container.remove(int)
public void removeAll()
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.
removeAll
in class Container
Container.add(java.awt.Component)
,
Container.remove(int)
,
Container.invalidate()
protected boolean isPaintingOrigin()
true
to cause painting to originate from JLayer
,
or one of its ancestors.isPaintingOrigin
in class JComponent
JComponent.isPaintingOrigin()
public void paintImmediately(int x, int y, int w, int h)
LayerUI.paintImmediately(int, int, int, int, JLayer)
method,
if LayerUI
is set.paintImmediately
in class JComponent
x
- the x value of the region to be paintedy
- the y value of the region to be paintedw
- the width of the region to be paintedh
- the height of the region to be paintedJComponent.repaint(long, int, int, int, int)
,
JComponent.isPaintingOrigin()
public void paint(Graphics g)
LayerUI
object.paint
in class JComponent
g
- the Graphics
to render toJComponent.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)
protected void paintComponent(Graphics g)
paint(Graphics)
and
ComponentUI.update(Graphics, JComponent)
methodspaintComponent
in class JComponent
g
- the Graphics
object to protectJComponent.paint(java.awt.Graphics)
,
ComponentUI
public boolean isOptimizedDrawingEnabled()
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.isOptimizedDrawingEnabled
in class JComponent
public void propertyChange(PropertyChangeEvent evt)
propertyChange
in interface PropertyChangeListener
evt
- A PropertyChangeEvent object describing the event source
and the property that has changed.public void setLayerEventMask(long layerEventMask)
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
.layerEventMask
- the bitmask of event types to receivegetLayerEventMask()
,
LayerUI.eventDispatched(AWTEvent, JLayer)
,
Component.isDisplayable()
public long getLayerEventMask()
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.JLayer
public void updateUI()
LayerUI.updateUI(JLayer)
method,
if LayerUI
is set.updateUI
in class JComponent
JComponent.setUI(javax.swing.plaf.ComponentUI)
,
UIManager.getLookAndFeel()
,
UIManager.getUI(javax.swing.JComponent)
public Dimension getPreferredScrollableViewportSize()
Scrollable
, this method delegates its
implementation to the view component.getPreferredScrollableViewportSize
in interface Scrollable
Scrollable
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
Scrollable
, this method delegates its
implementation to the view component.getScrollableBlockIncrement
in interface Scrollable
visibleRect
- The view area visible within the viewportorientation
- Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.direction
- Less than zero to scroll up/left, greater than zero for down/right.Scrollable
public boolean getScrollableTracksViewportHeight()
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.getScrollableTracksViewportHeight
in interface Scrollable
Scrollable
public boolean getScrollableTracksViewportWidth()
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.getScrollableTracksViewportWidth
in interface Scrollable
Scrollable
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
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.getScrollableUnitIncrement
in interface Scrollable
visibleRect
- The view area visible within the viewportorientation
- Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.direction
- Less than zero to scroll up/left, greater than zero for down/right.Scrollable
public void addNotify()
KeyboardAction
event listeners.
This method is called by the toolkit internally and should
not be called directly by programs.public void removeNotify()
KeyboardAction
s
set up in the the chain of parent components are removed.
This method is called by the toolkit internally and should
not be called directly by programs.public void doLayout()
LayerUI.doLayout(JLayer)
method,
if LayerUI
is set.doLayout
in class Container
LayoutManager.layoutContainer(java.awt.Container)
,
Container.setLayout(java.awt.LayoutManager)
,
Container.validate()
public AccessibleContext getAccessibleContext()
JLayer
.getAccessibleContext
in interface Accessible
getAccessibleContext
in class JComponent
JLayer
. Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2013, Oracle and/or its affiliates. All rights reserved.