The position of the knob relative to its track is proportionally equal to the position of the visible area relative to the client. Similarly, when the user moves the knob on the horizontal scroll bar to the right and left, the visible area of the client moves back and forth accordingly. When the user moves the knob on the vertical scroll bar up and down, the visible area of the client moves up and down. The following figure shows the three areas of a scroll bar: the knob (sometimes called the thumb), the (arrow) buttons, and the track. The scroll bars provide the interface for the user to manipulate the visible area. The viewport is responsible for positioning and sizing the client, based on the positions of the scroll bars, and displaying it.Ī scroll pane may use two separate instances of JViewport instance to manage the visible area of the client. This picture shows the relationship between the scroll pane and its client and indicates the classes that the scroll pane commissions to help: When the user manipulates the scroll bars in a scroll pane, the area of the client that is visible changes accordingly. If you need to refer to the client object again, you can either cache it in a variable or invoke getViewport().getViewportView() on the scroll pane. Note that JScrollPane has no corresponding getViewportView method. You can change the client dynamically by calling the setViewportView method. The scroll pane's client is also known as the view or viewport view. JScrollPane pictureScrollPane = new JScrollPane(picture) Where the member variables are declared: The ScrollDemo program establishes the scroll pane's client when creating the scroll pane: Notice that the scroll bars disappear when the scroll pane is large enough to display the entire image and reappear again when the scroll pane is too small to show the entire image. This scroll-by-dragging functionality is enabled by the scroll pane, and with the JComponent API, but it is implemented by the custom component that displays the image. The visible area of the image moves toward the cursor. Continuing to press the cursor, drag to a point outside the image and pause. Move the cursor over the image and press the cursor.Also, try clicking on the track above or below the knob on the vertical scroll bar, or to the left or right of the horizontal one. Click the arrow buttons on the scroll bars.The units on the row and column headers change to inches (or back to centimeters). Click the cm toggle in the upper left corner of the scroll pane.If you have a mouse with a wheel (which is generally between the mouse buttons) use the mouse wheel to scroll the image vertically.Watch the image scroll and the horizontal and vertical rulers scroll along. Alternatively, to compile and run the example yourself, consult the example index. Java™ Web Start ( download JDK 7 or later). Click the Launch button to run ScrollDemo using.The scroll pane also has two scroll bars, a row header, a column header, and four corners, three of which have been customized. Rather than displaying text, this scroll pane contains a image. The scroll pane in this application looks very different from the one in the previous demo program. Here is a snapshot of an application that uses a customized scroll pane to view a photograph: The rest of this section discusses the following topics: Refer to Sizing a Scroll Pane for information about techniques for making a scroll pane the size you want. If we didn't restrict the size of the scroll pane's container, the scroll pane would be big enough for the text area to display the full 5 rows and 30 columns specified with the JTextArea constructor. In the Java look and feel, this preferred size happens to be a bit less tall than required for the text area to display the 5 rows that we requested when creating it, so the scroll bar initially displays a vertical scroll bar. You might have noticed that the preceding code sets the preferred size of the scroll pane's container. The program doesn't invoke any methods on the JScrollPane object, since the scroll pane handles everything automatically: creating the scroll bars when necessary, redrawing the client when the user moves the scroll knobs, and so on. The boldface line of code creates the JScrollPane, specifying the text area as the scroll pane's client. SetPreferredSize(new Dimension(450, 110)) JScrollPane scrollPane = new JScrollPane(textArea) In a container that uses a BorderLayout:
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |