Class ScrollableJPanel

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

public class ScrollableJPanel extends JPanel implements Scrollable
This class extends the standard JPanel to have Scrollable characteristics, so extending this panel extremely large does not cause a containing JScrollPane to do so as well.
Author:
Andrew Pavlin, KA2DDO
See Also:
  • Constructor Details

    • ScrollableJPanel

      public ScrollableJPanel(LayoutManager layout, boolean isDoubleBuffered)
      Creates a new JPanel with the specified layout manager and buffering strategy.
      Parameters:
      layout - the LayoutManager to use
      isDoubleBuffered - a boolean, true for double-buffering, which uses additional memory space to achieve fast, flicker-free
    • ScrollableJPanel

      public ScrollableJPanel(LayoutManager layout)
      Create a new buffered JPanel with the specified layout manager
      Parameters:
      layout - the LayoutManager to use
    • ScrollableJPanel

      public ScrollableJPanel(boolean isDoubleBuffered)
      Creates a new JPanel with FlowLayout and the specified buffering strategy. If isDoubleBuffered is true, the JPanel will use a double buffer.
      Parameters:
      isDoubleBuffered - a boolean, true for double-buffering, which uses additional memory space to achieve fast, flicker-free updates
    • ScrollableJPanel

      public ScrollableJPanel()
      Creates a new JPanel with a double buffer and a flow layout.
  • Method Details

    • 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:
    • getDefaultScrollableSize

      public Dimension getDefaultScrollableSize()
      Get the default viewport size for scrolling this panel.
      Returns:
      default Dimension of the preferred viewport size
    • setDefaultScrollableSize

      public void setDefaultScrollableSize(Dimension defaultScrollableSize)
      Set the default viewport size for scrolling this panel.
      Parameters:
      defaultScrollableSize - default Dimension of the preferred viewport size
    • 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.