Class ScrollableJLabel

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, Scrollable

public class ScrollableJLabel extends JComponent implements Scrollable, Accessible
This widget adapts a JLabel to be displayable in a JScrollPane for multi-line read-only message displays.
Author:
Andrew Pavlin, KA2DDO
See Also:
  • Constructor Details

    • ScrollableJLabel

      public ScrollableJLabel(String text)
      Creates a JLabel instance with the specified text. The label is aligned against the leading edge of its display area, and centered vertically.
      Parameters:
      text - The text to be displayed by the label.
  • Method Details

    • setText

      public void setText(String text)
      Defines the text this component will display. If the value of text is null or empty string, nothing is displayed.

      The default value of this property is null.

      Parameters:
      text - String to display on label
    • getText

      public String getText()
      Returns the text string that the label displays.
      Returns:
      a String
      See Also:
    • paintComponent

      protected void paintComponent(Graphics g)
      Draw the text of this label on the screen.
      Overrides:
      paintComponent in class JComponent
      Parameters:
      g - the Graphics object to protect
    • getPreferredSize

      public Dimension getPreferredSize()
      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
    • getPreferredScrollableViewportSize

      public Dimension getPreferredScrollableViewportSize()
      Returns the preferred size of the viewport for a view component. For example, the preferred size of a JList component is the size required to accommodate all of the cells in its list. However, the value of preferredScrollableViewportSize is the size required for JList.getVisibleRowCount rows. A component without any properties that would affect the viewport size should just return getPreferredSize here.
      Specified by:
      getPreferredScrollableViewportSize in interface Scrollable
      Returns:
      the preferredSize of a JViewport whose view is this Scrollable
      See Also:
    • getScrollableUnitIncrement

      public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
      Components that display logical rows or columns should compute the scroll increment that will 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 JScrollPane, will use this method each time the user requests a unit scroll.

      Specified by:
      getScrollableUnitIncrement in interface Scrollable
      Parameters:
      visibleRect - The view area visible within the viewport
      orientation - Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
      direction - Less than zero to scroll up/left, greater than zero for down/right.
      Returns:
      The "unit" increment for scrolling in the specified direction. This value should always be positive.
      See Also:
    • getScrollableBlockIncrement

      public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
      Components that display logical rows or columns should compute the scroll increment that will completely expose one block of rows or columns, depending on the value of orientation.

      Scrolling containers, like JScrollPane, will use this method each time the user requests a block scroll.

      Specified by:
      getScrollableBlockIncrement in interface Scrollable
      Parameters:
      visibleRect - The view area visible within the viewport
      orientation - Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
      direction - Less than zero to scroll up/left, greater than zero for down/right.
      Returns:
      The "block" increment for scrolling in the specified direction. This value should always be positive.
      See Also:
    • getScrollableTracksViewportWidth

      public boolean getScrollableTracksViewportWidth()
      Return true if a viewport should always force the width of this Scrollable to match the width of the viewport. For example a normal text view that supported line wrapping would return true here, since it would be undesirable for wrapped lines to disappear beyond the right edge of the viewport. Note that returning true for a Scrollable whose ancestor is a JScrollPane effectively disables horizontal scrolling.

      Scrolling containers, like JViewport, will use this method each time they are validated.

      Specified by:
      getScrollableTracksViewportWidth in interface Scrollable
      Returns:
      True if a viewport should force the Scrollables width to match its own.
    • getScrollableTracksViewportHeight

      public boolean getScrollableTracksViewportHeight()
      Return true if a viewport should always force the height of this Scrollable to match the height of the viewport. For example a columnar text view that flowed text in left to right columns could effectively disable vertical scrolling by returning true here.

      Scrolling containers, like JViewport, will use this method each time they are validated.

      Specified by:
      getScrollableTracksViewportHeight in interface Scrollable
      Returns:
      True if a viewport should force the Scrollables height to match its own.
    • getLabelFor

      public Component getLabelFor()
      Get the component this is labelling.
      Returns:
      the Component this is labelling. Can be null if this does not label a Component. If the displayedMnemonic property is set and the labelFor property is also set, the label will call the requestFocus method of the component specified by the labelFor property when the mnemonic is activated.
    • setLabelFor

      public void setLabelFor(Component c)
      Set the component this is labelling. Can be null if this does not label a Component. If the displayedMnemonic property is set and the labelFor property is also set, the label will call the requestFocus method of the component specified by the labelFor property when the mnemonic is activated.
      Parameters:
      c - the Component this label is for, or null if the label is not the label for a component
    • getAccessibleContext

      public AccessibleContext getAccessibleContext()
      Returns the AccessibleContext associated with this JComponent. The method implemented by this base class returns null. Classes that extend JComponent should implement this method to return the AccessibleContext associated with the subclass.
      Specified by:
      getAccessibleContext in interface Accessible
      Overrides:
      getAccessibleContext in class Component
      Returns:
      the AccessibleContext of this JComponent