Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Specifying Corner Components

When a JScrollPane instance contains headers and scrollbars, there is a gap in the corners of the scroll pane where these components meet. A component can be specified that is placed into each corner with the setCorner method. This single method is used to set the component in any of the corners. The first parameter to this method must be one of the corner location constants defined in the ScrollPaneConstants interface. The constants are LOWER_LEFT_CORNER, LOWER_RIGHT_CORNER, UPPER_LEFT_CORNER, and UPPER_RIGHT_CORNER. Notice that these and the other scrollbar constants are defined in the ScrollPaneConstants interface, not the SwingConstants interface. The size of the corner component is determined by the available space due to the height and width of the headers and/or scrollbar that the corner is next to. The default scroll pane layout manager ignores the preferred size of the corner component itself. The current component defined for a corner can be queried with the getCorner method. The corner being queried is also specified with one of the corner constants defined in the ScrollPaneConstants interface. The corner components are bound properties in the JScrollPane class.

The following lines of code will add corner components to the ScrollPaneTest application. Each corner is a different color to make it obvious when each corner is visible. It is instructive to resize the application to display and hide the scrollbars to see when the corners are displayed. Removing one and then the other header and then both headers and executing the application will give all possible cases for corner components being displayed. The resulting scroll pane is shown in Figure 15.5.

JLabel lowerLeftCorner = new JLabel();
lowerLeftCorner.setOpaque( true );
lowerLeftCorner.setBackground( Color.blue );
scrollPane.setCorner( ScrollPaneConstants.LOWER_LEFT_CORNER,
                      lowerLeftCorner );

JLabel lowerRightCorner = new JLabel();
lowerRightCorner.setOpaque( true );
lowerRightCorner.setBackground( Color.yellow );
scrollPane.setCorner( ScrollPaneConstants.LOWER_RIGHT_CORNER,
                      lowerRightCorner );

JLabel upperLeftCorner = new JLabel();
upperLeftCorner.setOpaque( true );
upperLeftCorner.setBackground( Color.green );
scrollPane.setCorner( ScrollPaneConstants.UPPER_LEFT_CORNER,
                      upperLeftCorner );

JLabel upperRightCorner = new JLabel();
upperRightCorner.setOpaque( true );
upperRightCorner.setBackground( Color.red );
scrollPane.setCorner( ScrollPaneConstants.UPPER_RIGHT_CORNER,
                      upperRightCorner );


Figure 15.5  Corners added to the ScrollPaneTest application.


Note:  
A current bug in the ScrollPaneLayout class causes the wrong component to be removed from the layout. Removing the lower-right component will null the lower-left component, in effect removing the wrong component from the layout manager.

JComponent Scrolling Support

As we saw in Chapter 3, the JComponent class contains the autoscrolls property that is set with the setAutoscrolls method. When this property is true, an instance of the Autoscroller class is created for the component. This class extends the MouseAdapter utility class and overrides the mouseDragged method. This method scrolls the component in the viewport when the mouse is dragged.

When you are creating a custom component that will likely be contained in a scrollpane, the class should implement the Scrollable interface shown in Listing 15.2. The methods in this interface provide hints for the scrolling class. Currently four JFC classes implement this interface. They are the JTable, JTextComponent, JTree, and JList classes.


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.