Java Reference
In-Depth Information
Scrolling
11
In this chapter we see how to display long texts or large drawings that do not fit
into the window. Only a part of them can be made visible. So-called scroll panes
allow navigation in the text or the drawing and thus display different parts of the
document.
■
11.1
Scrolling text components
Scrolling
allows us to roll a long text upwards, downwards or sideways in a graph-
ical component or to change that part of a large drawing that is displayed. It is
not important what kind of document is displayed, whether it is text or a draw-
ing. Scrolling is realized as follows: imagine that the whole document is laid out
somewhere. A scrollable graphical component has a small area where a part of
the document is displayed. This area is called a
viewport
. The viewport can be
around the document with the help of
sliders
or
scroll bars
, thus changing the
part seen on the screen. See Figure 11.1. In Swing, scrolling is implemented by
class
JScrollPane
. The scroll bars are on the right and at the bottom by default.
We only use the following class
JScrollPane
:
JScrollPane(JComponent comp);
setHorizontalScrollBarPolicy(
int
policy)
setVerticalScrollBarPolicy(
int
policy)
JScrollPane(JComponent comp)
this constructor generates a
JScrollPane
, the
viewport of which displays part of
comp
.Ifthe whole component fits into
the viewport the scroll bars disappear. This behaviour can be changed using
the next method.
setHorizontalScrollBarPolicy(int policy)
determines when the scroll bars
are visible. Class
JScrollPane
defines constant values for
policy
:
HORIZON-
TAL_SCROLLBAR_ALWAYS
,
HORIZONTAL_SCROLLBAR_AS_NEEDED
and
HORIZON-
TAL_SCROLLBAR_NEVER
and have obvious meanings.
setVerticalScrollBarPolicy(int policy)
is the same as above, but for the
vertical scroll bar.
Search WWH ::
Custom Search