Class JXLabel
- All Implemented Interfaces:
ImageObserver
,MenuContainer
,Serializable
,Accessible
,SwingConstants
,Mnemonicable
- Direct Known Subclasses:
BasicHeaderUI.DescriptionPane
A JLabel
subclass which supports Painter
s, multi-line text,
and text rotation.
Painter support consists of the foregroundPainter
and backgroundPainter
properties. The
backgroundPainter
refers to a painter responsible for painting beneath the text and icon. This
painter, if set, will paint regardless of the opaque
property. If the background painter does not
fully paint each pixel, then you should make sure the opaque
property is set to false.
The foregroundPainter
is responsible for painting the icon and the text label. If no foregroundPainter
is specified, then the look and feel will paint the label. Note that if opaque is set to true and the look and feel
is rendering the foreground, then the foreground may paint over the background. Most look and feels will
paint a background when opaque
is true. To avoid this behavior, set opaque
to false.
Since JXLabel is not opaque by default (isOpaque()
returns false), neither of these problems
typically present themselves.
Multi-line text is enabled via the lineWrap
property. Simply set it to true. By default, line wrapping
occurs on word boundaries.
The text (actually, the entire foreground and background) of the JXLabel may be rotated. Set the
rotation
property to specify what the rotation should be. Specify rotation angle in radian units.
- Author:
- joshua.marinacci@sun.com, rbair, rah, mario_cesar
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interface
interface IValuestatic class
related to lineWrap supportstatic enum
Text alignment enums.Nested classes/interfaces inherited from class javax.swing.JLabel
JLabel.AccessibleJLabel
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final double
INVERTEDstatic final double
textOrientation value declaration NORMALboolean
paintedstatic final double
VERTICAL_LEFTstatic final double
VERTICAL_RIGHTFields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Fields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
-
Constructor Summary
ConstructorsConstructorDescriptionJXLabel()
Create a new JXLabel.Create a new JXLabel with the given text as the text for the label.Creates new JXLabel with given text and alignment.Creates new JXLabel with given text, icon and alignment.Creates new JXLabel with given icon.Creates new JXLabel with given icon and alignment. -
Method Summary
Modifier and TypeMethodDescriptionfinal Painter<? super JComponent>
Returns the current background painter.final Painter<? super JComponent>
Returns the current foregroundPainter.int
int
int
Returns the keyboard mnemonic for this component.protected JXLabel.MultiLineSupport
create MultiLineSupportprotected int
Gets current text wrapping style.double
Gets current value of text rotation in rads.int
getWidth()
boolean
Returns the current status of line wrap support.boolean
isOpaque()
boolean
Returns true if the background painter should paint where the border is or false if it should only paint inside the border.protected void
void
repaint()
void
repaint
(int x, int y, int width, int height) void
repaint
(long tm) void
repaint
(long tm, int x, int y, int width, int height) void
setBackgroundPainter
(Painter<? super JComponent> p) Sets a Painter to use to paint the background of this component By default there is already a single painter installed which draws the normal background for this component according to the current Look and Feel.void
setBounds
(int x, int y, int w, int h) void
setForegroundPainter
(Painter<? super JComponent> painter) Sets a new foregroundPainter on the label.void
setLineWrap
(boolean b) Enables line wrapping support for plain text.void
setMaxLineSpan
(int maxLineSpan) void
setMnemonic
(int mnemonic) Sets the keyboard mnemonic on this component.void
setPaintBorderInsets
(boolean paintBorderInsets) Sets the paintBorderInsets property.void
void
setTextAlignment
(JXLabel.TextAlignment alignment) Sets style of wrapping the text.void
setTextRotation
(double textOrientation) Sets new value for text rotation.Methods inherited from class javax.swing.JLabel
checkHorizontalKey, checkVerticalKey, getAccessibleContext, getDisabledIcon, getDisplayedMnemonic, getDisplayedMnemonicIndex, getHorizontalAlignment, getHorizontalTextPosition, getIcon, getIconTextGap, getLabelFor, getText, getUI, getUIClassID, getVerticalAlignment, getVerticalTextPosition, imageUpdate, paramString, setDisabledIcon, setDisplayedMnemonic, setDisplayedMnemonic, setDisplayedMnemonicIndex, setHorizontalAlignment, setHorizontalTextPosition, setIcon, setIconTextGap, setLabelFor, setUI, setVerticalAlignment, setVerticalTextPosition, updateUI
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, 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, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, 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, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
Methods inherited from class java.awt.Component
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, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, 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, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.jdesktop.swingx.Mnemonicable
getDisplayedMnemonicIndex, setDisplayedMnemonicIndex
-
Field Details
-
NORMAL
public static final double NORMALtextOrientation value declaration NORMAL- See Also:
-
INVERTED
public static final double INVERTEDINVERTED- See Also:
-
VERTICAL_LEFT
public static final double VERTICAL_LEFTVERTICAL_LEFT- See Also:
-
VERTICAL_RIGHT
public static final double VERTICAL_RIGHTVERTICAL_RIGHT- See Also:
-
painted
public boolean paintedpainted
-
-
Constructor Details
-
JXLabel
public JXLabel()Create a new JXLabel. This has the same semantics as creating a new JLabel. -
JXLabel
Creates new JXLabel with given icon.- Parameters:
image
- the icon to set.
-
JXLabel
Creates new JXLabel with given icon and alignment.- Parameters:
image
- the icon to set.horizontalAlignment
- the text alignment.
-
JXLabel
Create a new JXLabel with the given text as the text for the label. This is shorthand for:JXLabel label = new JXLabel(); label.setText("Some Text");
- Parameters:
text
- the text to set.
-
JXLabel
Creates new JXLabel with given text, icon and alignment.- Parameters:
text
- the test to set.image
- the icon to set.horizontalAlignment
- the text alignment relative to the icon.
-
JXLabel
Creates new JXLabel with given text and alignment.- Parameters:
text
- the test to set.horizontalAlignment
- the text alignment.
-
-
Method Details
-
getMnemonic
public int getMnemonic()Returns the keyboard mnemonic for this component.This property is used when the label is part of a larger component. If the labelFor property of the label is not null, the label will call the request Focus method of the component specified by the labelFor property when the mnemonic is activated.
- Specified by:
getMnemonic
in interfaceMnemonicable
- Returns:
- the keyboard mnemonic
-
setMnemonic
public void setMnemonic(int mnemonic) Sets the keyboard mnemonic on this component. The mnemonic is the key which when combined with the look and feel's mouseless modifier (usually Alt) will activate this component.A mnemonic must correspond to a single key on the keyboard and should be specified using one of the
VK_XXX
keycodes defined injava.awt.event.KeyEvent
. Mnemonics are case-insensitive, therefore a key event with the corresponding keycode would cause the button to be activated whether or not the Shift modifier was pressed.- Specified by:
setMnemonic
in interfaceMnemonicable
- Parameters:
mnemonic
- the key code which represents the mnemonic- See Also:
-
getForegroundPainter
Returns the current foregroundPainter. This is a bound property. By default the foregroundPainter will be an internal painter which executes the standard painting code (paintComponent()).- Returns:
- the current foreground painter.
-
setText
-
setBounds
public void setBounds(int x, int y, int w, int h) -
setForegroundPainter
Sets a new foregroundPainter on the label. This will replace the existing foreground painter. Existing painters can be wrapped by using a CompoundPainter.- Parameters:
painter
- the foregroundPainter
-
setBackgroundPainter
Sets a Painter to use to paint the background of this component By default there is already a single painter installed which draws the normal background for this component according to the current Look and Feel. CallingsetBackgroundPainter
will replace that existing painter.- Parameters:
p
- the new painter- See Also:
-
getBackgroundPainter
Returns the current background painter. The default value of this property is a painter which draws the normal JPanel background according to the current look and feel.- Returns:
- the current painter
- See Also:
-
getTextRotation
public double getTextRotation()Gets current value of text rotation in rads.- Returns:
- a double representing the current rotation of the text
- See Also:
-
getPreferredSize
- Overrides:
getPreferredSize
in classJComponent
-
getMaxLineSpan
public int getMaxLineSpan()- Returns:
- int maxLineSpan
-
setMaxLineSpan
public void setMaxLineSpan(int maxLineSpan) - Parameters:
maxLineSpan
- int
-
setTextRotation
public void setTextRotation(double textOrientation) Sets new value for text rotation. The value can be anything in range [0,2PI]. Note that although property name suggests only text rotation, the whole foreground painter is rotated in fact. Due to various reasons it is strongly discouraged to access any size related properties of the label from other threads then EDT when this property is set.- Parameters:
textOrientation
- Value for text rotation in range [0,2PI]- See Also:
-
setLineWrap
public void setLineWrap(boolean b) Enables line wrapping support for plain text. By default this support is disabled to mimic default of the JLabel. Value of this property has no effect on HTML text.- Parameters:
b
- the new value
-
isLineWrap
public boolean isLineWrap()Returns the current status of line wrap support. The default value of this property is false to mimic default JLabel behavior. Value of this property has no effect on HTML text.- Returns:
- the current multiple line splitting status
-
getTextAlignment
Gets current text wrapping style.- Returns:
- the text alignment for this label
-
setTextAlignment
Sets style of wrapping the text.- Parameters:
alignment
- property "textAlignment"- See Also:
-
isPaintBorderInsets
public boolean isPaintBorderInsets()Returns true if the background painter should paint where the border is or false if it should only paint inside the border. This property is true by default. This property affects the width, height, and initial transform passed to the background painter.- Returns:
- current value of the paintBorderInsets property
-
isOpaque
public boolean isOpaque()- Overrides:
isOpaque
in classJComponent
-
setPaintBorderInsets
public void setPaintBorderInsets(boolean paintBorderInsets) Sets the paintBorderInsets property. Set to true if the background painter should paint where the border is or false if it should only paint inside the border. This property is true by default. This property affects the width, height, and initial transform passed to the background painter. This is a bound property.- Parameters:
paintBorderInsets
- new value of the paintBorderInsets property
-
paintComponent
- Overrides:
paintComponent
in classJComponent
- Parameters:
g
- graphics to paint on
-
repaint
public void repaint() -
repaint
public void repaint(int x, int y, int width, int height) -
repaint
public void repaint(long tm) -
repaint
public void repaint(long tm, int x, int y, int width, int height) - Overrides:
repaint
in classJComponent
-
getHeight
public int getHeight()- Overrides:
getHeight
in classJComponent
-
getWidth
public int getWidth()- Overrides:
getWidth
in classJComponent
-
getMultiLineSupport
create MultiLineSupport- Returns:
- instance of MultiLineSupport
-
getOccupiedWidth
protected int getOccupiedWidth()- Returns:
- int occupiedWidth
-