ScrollablePanel 6.0
Download

ScrollablePanel 6.0

When using a JScrollPane the general rule is that the scrollbars will appear
Rating
Your vote:
Latest version:
6.0 See all
Developer:
Screenshots
1 / 1
Demo
Download

When using a JScrollPane the general rule is that the scrollbars will appear when the “preferred size” of the component added to the JViewport of the scroll pane is greater than the “size” of the viewport. There may be times when you wish to prevent a scrollbar from appearing even though the preferred size is greater than the size. To prevent the horizontal scrollbar from appearing the first solution you might attempt would be to use the setHorizontalScrollBarPolicy() method of JScrollPane with a value of ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER. This works, in the sense that the scrollbar is not displayed, but the problem is that the view of the component is just truncated in the viewport. A more flexible approach is to implement the Scrollable interface on your component. In this solution you would want to override the getScrollableTracksViewportWidth() method to return “true”. The width of the component is now set to be the width of the viewport (instead of the preferred width of the component). This can affect the way the layout of the component is done (in the case of using a JPanel) or it can affect the way a component is painted (in the case of a JTextArea using wrapping). However, the Scrollable interface specifies 5 different methods and since many components don’t implement this interface you will usually end up implementing all 5 methods. The purpose of the ScrollablePanel class is to provide reasonable implementations for each of the Scrollable methods and to support easy customization of the Scrollable behaviour. The Scrollable interface only provides a boolean value for determining whether or not the viewport size (width or height) should be used by the scrollpane when determining if scrollbars should be made visible. ScrollablePanel supports this as well as adding the concept of dynamically changing this value based on the size of the viewport. In this case the viewport size will only be used when it is larger than the panels size. This has the effect of ensuring the viewport is always full as components added to the panel will be sized to fill the area available, based on the rules of the applicable layout manager of course.

Comments

User

Your vote: