![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Scrolling PolicyHow 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 components size extends beyond the viewports 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 classs 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 HeadingsThe 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 doesnt 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.
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" ) );
|
![]() |
|