![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
It is also possible to programmatically alter which portion of the underlying component is visible in the viewport. The setViewPosition method defines which point of the component will be located in the upper-left corner of the viewport. The setExtentSize method will define the size of the visible portion of the component. The setViewSize method will set the size of the underlying component. Changing either the view position or the extent size causes the viewport to notify change listeners that a change has occurred. However, the exact nature of the change is not delivered with the change event. The listener must query the viewport for its new state. The view can also be scrolled by using the scrollRectToVisible method. This method ensures that the given rectangle is visible. ViewportLayout Layout ManagerThe default behavior of the JViewport class is to create an instance of the ViewportLayout class to manage its single child component. The ViewportLayout class assumes it is managing a JViewport instance, so it is not a general-purpose layout manager. The layout manager can be set to a different layout manager if required. Also, extensions to the JViewport class can override the createLayoutManager method to return a different class of LayoutManager for the viewport. The ViewportLayout layout manager positions components that are smaller than the viewport in the upper-left corner, leaving extra space below or to the right of the component. However, if the origin of the component is displayed and the component is smaller than the viewport in either direction, the component is resized to the size of the viewport in that direction. If both directions are smaller than the viewport, the component will be resized to the viewports size. If the component implements the Scrollable interface and is set to track the viewports size, the component is sized to the viewports size. The JScrollPane ClassThe JFC class that is typically used for scrolling a component is the JScrollPane class. The JScrollPane class is an extension of the JComponent class and manages a JViewport instance, scrollbars, and viewport headings. The JViewport class manages the component that is to be scrolled and was described in the previous section. Instances of the JScrollBar class, described in Chapter 10, JList, JComboBox, and Bound Controls, are used for the scrollbars. The heading instances are also of the JViewport class. One heading can be specified for the horizontal axis and another for the vertical axis. The JScrollPane class manages a single viewport in the center of its area. The viewport is specified by using the setViewport method. Instead of specifying the viewport, the component to be scrolled can be specified by using the setViewportView method. In this case, the JScrollPane class creates the viewport itself. By default, an instance of the JViewport class is used for the viewport. However, extensions to the JScrollPane class can override the createViewport method to return an extension of the JViewport class to be used. The getViewport method can be used to query the current viewport in the JScrollPane instance. This method will return null if there is not a viewport defined in the scroll pane. The viewport is a bound property of the JScrollPane class. Listing 15.1 shows a simple application that displays JLabel instances stacked in a box. The version of the JScrollPane constructor taking the view component as a parameter is used. The resulting application is shown in Figure 15.3. Listing 15.1 The ScrollPaneTest Application package com.foley.test; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import com.foley.utility.ApplicationFrame; import com.foley.utility.JBox; /** * An application that displays a large component * in a JScrollPane. * * @author Mike Foley **/ public class ScrollPaneTest extends Object { /** * Application entry point. * Create a frame, the component, scrollpane and display it. * * @param args Command line parameter. Not used. **/ public static void main( String args[] ) { JFrame frame = new ApplicationFrame( "ScrollPane Test" ); Font ourFont = new Font( "Helvitica", Font.BOLD, 24 ); JBox box = JBox.createVerticalJBox(); JLabel label = new JLabel(); label.setText( "This is a long message that will not" + " fit on may displays. " + "Better scroll it!" ); label.setFont( ourFont ); box.add( label ); label = new JLabel(); label.setText( "The JBox contains many labels." ); label.setFont( ourFont ); box.add( label ); label = new JLabel(); label.setText( "Each label is stacked on top of" + " each other in a JBox container." ); label.setFont( ourFont ); box.add( label ); label = new JLabel(); label.setText( "It may be too tall as well as too wide." ); label.setFont( ourFont ); box.add( label ); label = new JLabel(); label.setText( "Better scroll it!" ); label.setFont( ourFont ); label.setAutoscrolls( true ); box.add( label ); JScrollPane scrollPane = new JScrollPane( box ); frame.getContentPane().add( scrollPane, BorderLayout.CENTER ); frame.pack(); frame.setVisible( true ); } // main } // ScrollPaneTest
|
![]() |
|