org.jdesktop.swingx
Class JXLabel

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JLabel
                  extended by org.jdesktop.swingx.JXLabel
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, SwingConstants
Direct Known Subclasses:
BasicHeaderUI.DescriptionPane

public class JXLabel
extends JLabel

A JLabel subclass which supports Painters, 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.

See Also:
Serialized Form

Nested Class Summary
protected static interface JXLabel.IValue
           
static class JXLabel.MultiLineSupport
           
static class JXLabel.TextAlignment
          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
static double INVERTED
           
static double NORMAL
           
 boolean painted
           
static double VERTICAL_LEFT
           
static double VERTICAL_RIGHT
           
 
Fields inherited from class javax.swing.JLabel
labelFor
 
Fields inherited from class javax.swing.JComponent
accessibleContext, 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
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
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
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
JXLabel()
          Create a new JXLabel.
JXLabel(Icon image)
          Creates new JXLabel with given icon.
JXLabel(Icon image, int horizontalAlignment)
          Creates new JXLabel with given icon and alignment.
JXLabel(String text)
          Create a new JXLabel with the given text as the text for the label.
JXLabel(String text, Icon image, int horizontalAlignment)
          Creates new JXLabel with given text, icon and alignment.
JXLabel(String text, int horizontalAlignment)
          Creates new JXLabel with given text and alignment.
 
Method Summary
 Painter getBackgroundPainter()
          Returns the current background painter.
 Painter getForegroundPainter()
          Returns the current foregroundPainter.
 int getHeight()
          Returns the current height of this component.
 int getMaxLineSpan()
           
protected  JXLabel.MultiLineSupport getMultiLineSupport()
           
protected  int getOccupiedWidth()
           
 Dimension getPreferredSize()
          If the preferredSize has been set to a non-null value just returns it.
 JXLabel.TextAlignment getTextAlignment()
          Gets current text wrapping style.
 double getTextRotation()
          Gets current value of text rotation in rads.
 int getWidth()
          Returns the current width of this component.
 boolean isLineWrap()
          Returns the current status of line wrap support.
 boolean isOpaque()
          Returns true if this component is completely opaque.
 boolean isPaintBorderInsets()
          Returns true if the background painter should paint where the border is or false if it should only paint inside the border.
protected  void paintComponent(Graphics g)
          Calls the UI delegate's paint method, if the UI delegate is non-null.
 void repaint()
          Repaints this component.
 void repaint(int x, int y, int width, int height)
          Repaints the specified rectangle of this component.
 void repaint(long tm)
          Repaints the component.
 void repaint(long tm, int x, int y, int width, int height)
          Adds the specified region to the dirty region list if the component is showing.
 void reshape(int x, int y, int w, int h)
           
 void setBackgroundPainter(Painter 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 setForegroundPainter(Painter painter)
          Sets a new foregroundPainter on the label.
 void setLineWrap(boolean b)
          Enables line wrapping support for plain text.
 void setMaxLineSpan(int maxLineSpan)
           
 void setPaintBorderInsets(boolean paintBorderInsets)
          Sets the paintBorderInsets property.
 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, setText, 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, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOptimizedDrawingEnabled, isPaintingForPrint, 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, 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, transferFocusBackward, 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, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, 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, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NORMAL

public static final double NORMAL
See Also:
Constant Field Values

INVERTED

public static final double INVERTED
See Also:
Constant Field Values

VERTICAL_LEFT

public static final double VERTICAL_LEFT
See Also:
Constant Field Values

VERTICAL_RIGHT

public static final double VERTICAL_RIGHT
See Also:
Constant Field Values

painted

public boolean painted
Constructor Detail

JXLabel

public JXLabel()
Create a new JXLabel. This has the same semantics as creating a new JLabel.


JXLabel

public JXLabel(Icon image)
Creates new JXLabel with given icon.

Parameters:
image - the icon to set.

JXLabel

public JXLabel(Icon image,
               int horizontalAlignment)
Creates new JXLabel with given icon and alignment.

Parameters:
image - the icon to set.
horizontalAlignment - the text alignment.

JXLabel

public JXLabel(String text)
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

public JXLabel(String text,
               Icon image,
               int horizontalAlignment)
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

public JXLabel(String text,
               int horizontalAlignment)
Creates new JXLabel with given text and alignment.

Parameters:
text - the test to set.
horizontalAlignment - the text alignment.
Method Detail

getForegroundPainter

public final Painter 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.

reshape

public void reshape(int x,
                    int y,
                    int w,
                    int h)
Overrides:
reshape in class JComponent
Parameters:
x - the new horizontal location
y - the new vertical location
w - the new width
h - the new height
See Also:
Component.setBounds(int, int, int, int)

setForegroundPainter

public void setForegroundPainter(Painter painter)
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 -

setBackgroundPainter

public void setBackgroundPainter(Painter 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. Calling setBackgroundPainter will replace that existing painter.

Parameters:
p - the new painter
See Also:
getBackgroundPainter()

getBackgroundPainter

public final Painter 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:
setBackgroundPainter(Painter)

getTextRotation

public double getTextRotation()
Gets current value of text rotation in rads.

Returns:
a double representing the current rotation of the text
See Also:
setTextRotation(double)

getPreferredSize

public Dimension getPreferredSize()
Description copied from class: javax.swing.JComponent
If the preferredSize has been set to a non-null value just returns it. If the UI delegate's getPreferredSize method returns a non null value then return that; otherwise defer to the component's layout manager.

Overrides:
getPreferredSize in class JComponent
Returns:
the value of the preferredSize property
See Also:
JComponent.setPreferredSize(java.awt.Dimension), ComponentUI

getMaxLineSpan

public int getMaxLineSpan()

setMaxLineSpan

public void setMaxLineSpan(int maxLineSpan)

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:
getTextRotation()

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

public JXLabel.TextAlignment getTextAlignment()
Gets current text wrapping style.

Returns:

setTextAlignment

public void setTextAlignment(JXLabel.TextAlignment alignment)
Sets style of wrapping the text.

Parameters:
alignment -
See Also:
for accepted values.

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 intial transform passed to the background painter.

Returns:
current value of the paintBorderInsets property

isOpaque

public boolean isOpaque()
Description copied from class: javax.swing.JComponent
Returns true if this component is completely opaque.

An opaque component paints every pixel within its rectangular bounds. A non-opaque component paints only a subset of its pixels or none at all, allowing the pixels underneath it to "show through". Therefore, a component that does not fully paint its pixels provides a degree of transparency.

Subclasses that guarantee to always completely paint their contents should override this method and return true.

Overrides:
isOpaque in class JComponent
Returns:
true if this component is completely opaque
See Also:
JComponent.setOpaque(boolean)

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

protected void paintComponent(Graphics g)
Description copied from class: javax.swing.JComponent
Calls the UI delegate's paint method, if the UI delegate is non-null. We pass the delegate a copy of the Graphics object to protect the rest of the paint code from irrevocable changes (for example, Graphics.translate).

If you override this in a subclass you should not make permanent changes to the passed in Graphics. For example, you should not alter the clip Rectangle or modify the transform. If you need to do these operations you may find it easier to create a new Graphics from the passed in Graphics and manipulate it. Further, if you do not invoker super's implementation you must honor the opaque property, that is if this component is opaque, you must completely fill in the background in a non-opaque color. If you do not honor the opaque property you will likely see visual artifacts.

The passed in Graphics object might have a transform other than the identify transform installed on it. In this case, you might get unexpected results if you cumulatively apply another transform.

Overrides:
paintComponent in class JComponent
Parameters:
g - graphics to paint on
See Also:
JComponent.paint(java.awt.Graphics), ComponentUI

repaint

public void repaint()
Description copied from class: java.awt.Component
Repaints this component.

If this component is a lightweight component, this method causes a call to this component's paint method as soon as possible. Otherwise, this method causes a call to this component's update method as soon as possible.

Note: For more information on the paint mechanisms utilitized by AWT and Swing, including information on how to write the most efficient painting code, see Painting in AWT and Swing.

Overrides:
repaint in class Component
See Also:
Component.update(Graphics)

repaint

public void repaint(int x,
                    int y,
                    int width,
                    int height)
Description copied from class: java.awt.Component
Repaints the specified rectangle of this component.

If this component is a lightweight component, this method causes a call to this component's paint method as soon as possible. Otherwise, this method causes a call to this component's update method as soon as possible.

Note: For more information on the paint mechanisms utilitized by AWT and Swing, including information on how to write the most efficient painting code, see Painting in AWT and Swing.

Overrides:
repaint in class Component
Parameters:
x - the x coordinate
y - the y coordinate
width - the width
height - the height
See Also:
Component.update(Graphics)

repaint

public void repaint(long tm)
Description copied from class: java.awt.Component
Repaints the component. If this component is a lightweight component, this results in a call to paint within tm milliseconds.

Note: For more information on the paint mechanisms utilitized by AWT and Swing, including information on how to write the most efficient painting code, see Painting in AWT and Swing.

Overrides:
repaint in class Component
Parameters:
tm - maximum time in milliseconds before update
See Also:
Component.paint(java.awt.Graphics), Component.update(Graphics)

repaint

public void repaint(long tm,
                    int x,
                    int y,
                    int width,
                    int height)
Description copied from class: javax.swing.JComponent
Adds the specified region to the dirty region list if the component is showing. The component will be repainted after all of the currently pending events have been dispatched.

Overrides:
repaint in class JComponent
Parameters:
tm - this parameter is not used
x - the x value of the dirty region
y - the y value of the dirty region
width - the width of the dirty region
height - the height of the dirty region
See Also:
Component.isShowing(), RepaintManager.addDirtyRegion(javax.swing.JComponent, int, int, int, int)

getHeight

public int getHeight()
Description copied from class: javax.swing.JComponent
Returns the current height of this component. This method is preferable to writing component.getBounds().height, or component.getSize().height because it doesn't cause any heap allocations.

Overrides:
getHeight in class JComponent
Returns:
the current height of this component

getWidth

public int getWidth()
Description copied from class: javax.swing.JComponent
Returns the current width of this component. This method is preferable to writing component.getBounds().width, or component.getSize().width because it doesn't cause any heap allocations.

Overrides:
getWidth in class JComponent
Returns:
the current width of this component

getMultiLineSupport

protected JXLabel.MultiLineSupport getMultiLineSupport()

getOccupiedWidth

protected int getOccupiedWidth()