Java Reference
In-Depth Information
Display 18.7 Some Methods in the Class JScrollPane (part 2 of 2)
public void setVerticalScrollBarPolicy( int policy)
Sets the policy for showing the vertical scroll bar. The policy should be one of
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
JScrollPane.VERTICAL_SCROLLBAR_NEVER
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
The phrase AS_NEEDED means the scroll bar is shown only when it is needed. This is explained more
fully in the text. The meanings of the other policy constants are obvious from their names.
(As indicated, these constants are defined in the class JScrollPane . You should not need to even be
aware of the fact that they have int values. Think of them as policies, not as int values.)
The JScrollPane can then be added to a container, such as a JPanel or JFrame , as
follows:
textPanel.add(scrolledText);
This is illustrated by the program in Display 18.8.
Note the following two lines in the constructor definition in Display 18.8:
setting scroll
bar policies
scrolledText.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrolledText.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Despite the imposing length of these two method invocations, they perform a very
simple task. The first merely specifies that the horizontal scroll bar will always be
present. The second specifies that the vertical scroll bar will always be present.
If you omit the invocation of the two methods setHorizontalScrollBarPolicy
and setVerticalScrollBarPolicy , the scroll bars will be visible only when you need
them. In other words, if you omit these two method invocations and all the text fits in
the view port, then no scroll bars will be visible. When you add enough text to need
scroll bars, the needed scroll bars will appear automatically.
Display 18.7 summarizes what we have said about the class JScrollPane . We are
interested in using JScrollPane only with text areas. However, as we note in Display
18.7, JScrollPane can be used with almost any sort of component.
 
Search WWH ::




Custom Search