Class JXLabel

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, SwingConstants, BackgroundPaintable
Direct Known Subclasses:
BasicHeaderUI.DescriptionPane

@JavaBean public class JXLabel extends JLabel implements BackgroundPaintable

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:
  • Field Details

    • NORMAL

      public static final double NORMAL
      See Also:
    • INVERTED

      public static final double INVERTED
      See Also:
    • VERTICAL_LEFT

      public static final double VERTICAL_LEFT
      See Also:
    • VERTICAL_RIGHT

      public static final double VERTICAL_RIGHT
      See Also:
    • textRotation

      private double textRotation
    • painting

      private boolean painting
    • foregroundPainter

      private Painter foregroundPainter
    • backgroundPainter

      private Painter backgroundPainter
    • multiLine

      private boolean multiLine
    • pWidth

      private int pWidth
    • pHeight

      private int pHeight
    • dontIgnoreRepaint

      private boolean dontIgnoreRepaint
    • occupiedWidth

      private int occupiedWidth
    • oldRendererKey

      private static final String oldRendererKey
      See Also:
    • paintBorderInsets

      private boolean paintBorderInsets
    • maxLineSpan

      private int maxLineSpan
    • painted

      public boolean painted
    • textAlignment

      private JXLabel.TextAlignment textAlignment
  • Constructor Details

    • 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 Details

    • initPainterSupport

      private void initPainterSupport()
    • initLineWrapSupport

      private void initLineWrapSupport()
      Helper method for initializing multi line support.
    • 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
    • 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.
      Specified by:
      setBackgroundPainter in interface BackgroundPaintable
      Parameters:
      p - the new painter
      See Also:
    • 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.
      Specified by:
      getBackgroundPainter in interface BackgroundPaintable
      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

      public Dimension getPreferredSize()
      Overrides:
      getPreferredSize in class JComponent
    • getWrappingView

      private View getWrappingView()
    • getViewport

      private Container getViewport()
    • calculateIconRect

      private Rectangle calculateIconRect()
    • getMaxLineSpan

      public int getMaxLineSpan()
    • setMaxLineSpan

      public void setMaxLineSpan(int maxLineSpan)
    • rotateWidth

      private static int rotateWidth(Dimension size, double theta)
    • rotateHeight

      private static int rotateHeight(Dimension size, double theta)
    • setTextRotation

      public void setTextRotation(double textOrientation)
      Sets new value for text rotation. The value can be anything in range invalid input: '<'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 invalid input: '<'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

      public JXLabel.TextAlignment getTextAlignment()
      Gets current text wrapping style.
      Returns:
      the text alignment for this label
    • setTextAlignment

      public void setTextAlignment(JXLabel.TextAlignment alignment)
      Sets style of wrapping the text.
      Parameters:
      alignment -
      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.
      Specified by:
      isPaintBorderInsets in interface BackgroundPaintable
      Returns:
      current value of the paintBorderInsets property
    • isOpaque

      public boolean isOpaque()
      Overrides:
      isOpaque in class JComponent
    • 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.
      Specified by:
      setPaintBorderInsets in interface BackgroundPaintable
      Parameters:
      paintBorderInsets - new value of the paintBorderInsets property
    • paintComponent

      protected void paintComponent(Graphics g)
      Overrides:
      paintComponent in class JComponent
      Parameters:
      g - graphics to paint on
    • calculateT

      private Point2D calculateT()
    • repaint

      public void repaint()
      Overrides:
      repaint in class Component
    • repaint

      public void repaint(int x, int y, int width, int height)
      Overrides:
      repaint in class Component
    • repaint

      public void repaint(long tm)
      Overrides:
      repaint in class Component
    • repaint

      public void repaint(long tm, int x, int y, int width, int height)
      Overrides:
      repaint in class JComponent
    • getHeight

      public int getHeight()
      Overrides:
      getHeight in class JComponent
    • getWidth

      public int getWidth()
      Overrides:
      getWidth in class JComponent
    • getMultiLineSupport

      protected JXLabel.MultiLineSupport getMultiLineSupport()
    • getOccupiedWidth

      protected int getOccupiedWidth()