Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Visual Basic Insiders Technical Summit
Visual Basic Insiders Technical Summit
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Scrolling Policy

How the JScrollPane class displays its scrollbars can be configured. The scrollbars can always be displayed, never displayed, or displayed as required. The “as required” policy will display scrollbars when the center component’s size extends beyond the viewport’s size and hide them when the component is smaller than this size. The horizontal and vertical scrollbars are hidden and shown independently of each other.

Specifying the scrolling policy for instances of the JScrollPane class is done independently for the horizontal and vertical directions. The setHorizontalScrollBarPolicy and setVerticalScrollBarPolicy methods are used to set the policy for each direction. Both of these methods take a single parameter that is a constant defined in the ScrollPaneConstants interface. Allowed values for the setVerticalScrollBarPolicy method are VERTICAL_SCROLLBAR_AS_NEEDED, VERTICAL_SCROLLBAR_ALWAYS, and VERTICAL_SCROLLBAR_NEVER. Similarly, for the setHorizontalScrollBarPolicy method the defined constants for the scrolling policy are HORIZONTAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_ALWAYS, and HORIZONTAL_SCROLLBAR_NEVER. There are also versions of the JScrollPane class’s constructor that allow the scrolling policy to be specified. The scrolling policy for each direction can be independently queried by using the getHorizontalScrollBarPolicy and getVerticalScrollBarPolicy methods. The default scrolling policy is “as needed” in both directions. The scrolling policy of each direction is a bound property of the JScrollPane class.

A handle to each individual scrollbar can be obtained via the getHorizontalScrollBar and getVerticalScrollBar methods. Once this handle is obtained, the methods in the JScrollBar class can be called directly. For example, it is often desirable to set the unit and block increments via the setUnitIncrement and setBlockIncrement methods defined in the JScrollBar class for the scrollbars.

Specifying Row and Column Headings

The methods to set the headings in a scroll pane are named setRowHeader and setColumnHeader. These names suggest tabular data in the scroll pane, but this does not have to be the case. These methods take an instance of the JViewport class and set it as the header in the appropriate direction. When a column header view is set, it scrolls right and left in sync with the main view, but it doesn’t scroll up or down. Similarly, a row header view scrolls up and down with the main view, but does not scroll right or left. The current headers can be queried via the getRowHeader and getColumnHeader methods. Each of these methods returns null if a header has not been specified for that axis. Both the rowHeader and columnHeader properties are bound in the JScrollPane class.

As was mentioned in the previous paragraph, the setRowHeader and setColumnHeader methods require a viewport as their parameter. The setRowHeaderView and setColumnHeaderView methods allow any component to be specified for the header. These methods will create a viewport if necessary and will set the given component as the view for the viewport. If a column header had been previously specified, it is reused by setting its view property to the component passed as the argument to these methods.


Note:  
Using either of the setRowHeaderView or setColumnHeaderView methods can cause difficulty when replacing a header. Since the existing viewport is reused, a view header property change event is not fired by the JScrollPane class. Since the JViewport class does not fire a property change event when its view is changed, there is no way to be informed of this change. This condition is exacerbated by the fact that a property change event is fired if a header has not already been set for the scroll pane. This appears to be a bug rather than a feature.

Header views can be added to the ScrollPaneTest application presented earlier in this chapter by adding the following lines of code after the scroll pane has been created. The resulting scroll pane is shown in Figure 15.4.

scrollPane.setColumnHeaderView(
                new JLabel( "Column Header", SwingConstants.CENTER )
);scrollPane.setRowHeaderView( new JLabel( "Row Header" ) );


Figure 15.4  Headers added to the ScrollPaneTest application.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.