Java Reference
In-Depth Information
It might seem strange for the pack() and setVisible() methods to
be called outside the constructor method of the frame. Because
these methods are public, there's no prohibition against calling
these (and other) methods inside or outside an interface compo-
nent's class.
NOTE
Scroll Panes
As you learned in yesterday's lesson, in early versions of Java, some components (such
as text areas) had a built-in scrollbar. The bar could be used when the text in the compo-
nent took up more space than the component could display. Scrollbars could be used in
either the vertical or horizontal direction to scroll through the text.
One of the most common examples of scrolling is in a web browser, where a scrollbar
can be used on any page bigger than the browser's display area.
10
Swing changes the rules for scrollbars to the following:
For a component to be able to scroll, it must be added to a JScrollPane container.
n
This JScrollPane container is added to a container in place of the scrollable com-
ponent.
n
Scroll panes can be created using the ScrollPane( Object ) constructor, where Object
represents the component that can be scrolled.
The following example creates a text area in a scroll pane and adds the scroll pane,
scroller , to a container called mainPane :
textBox = new JTextArea(7, 30);
JScrollPane scroller = new JScrollPane(textBox);
mainPane.add(scroller);
As you're working with scroll panes, it can often be useful to indicate the size you want
it to occupy on the interface. This is done by calling the setPreferredSize( Dimension )
method of the scroll pane before it is added to a container. The Dimension object repre-
sents the width and height of the preferred size, represented in pixels.
The following code builds on the previous example by setting the preferred size of
scroller :
Dimension pref = new Dimension(350, 100);
scroller.setPreferredSize(pref);
This should be handled before scroller is added to a container.
Search WWH ::




Custom Search