Java Reference
In-Depth Information
Figure 11.3 The drawing display application. The vertical scroll bar is visible because the
height of the viewport is less than the preferred height of the panel to display. The horizontal
scroll bar is not visible because the viewport is wider than the preferred width of the panel.
The vertical line is the right edge of the preferred area. The drawing outside the preferred area
is not considered by the runtime system when determining whether to show the scroll bar
As an example we construct a panel and add some drawing commands to its
method paintComponent . The resulting class is called DrawingDisplayScroll-
Panel . The size of the panel is set to 250
250 pixels. We draw a red rectangle
to indicate the border of this area and a text to denote it. We then draw a cir-
cle and a text outside the area specified in the setPreferredSize command. We
then create a scroll pane that displays the panel in its viewport. The scroll pane
is embedded into a frame. We list class DrawingDisplayScrollPanel below but
omit the listing of the frame class DrawingDisplayScrollFrame and the driver
DrawingDisplayScrollDriver ; both can be downloaded from the topic's home
page. Try resizing the frame in different ways to see when scroll bars appear or
vanish.
Running this application and resizing the frame will show that the scroll bars
become visible if the preferred size area of the panel does not fit into the viewport.
The result can be seen in Figure 11.3. The runtime system does not recognize that
×
!
there is some drawing outside this area. The programmer must take care of that
by monitoring the current size of the drawing and adjusting the preferred size. In
Chapter 21 we shall learn an elegant way to do this.
File: its/Scrolling/DrawingDisplayScrollPanel.java
package its.Scrolling;
1.
2.
3.
import java.awt.Color;
import java.awt.Dimension;
4.
import java.awt.Graphics;
5.
import javax.swing.JPanel;
6.
Search WWH ::




Custom Search