Add a Scroll Bar
Learn to add a scroll bar in the text area and change the background color.
Adding scroll bars
Another way to fit a large text area into a smaller space is by adding scroll bars.
- Remove the rows and columns parameter values from the call to the constructor for
disclaimerTextArea
. - Remove the line of code that added the
disclaimerTextArea
to the bottom of the window. - Create a new
JScrollPane
object calledscrollPane
, initialized by calling JScrollPane’s constructor withdisclaimerTextArea
as the parameter value. - Create a new
Dimension
object calledsize
that is 300 wide and 50 high. - Set the preferred size of
scrollPane
tosize
using JScrollPane’ssetPreferredSize()
method. - Add
scrollPane
to the bottom of the window using BorderLayout’sPAGE_END
variable.
public WizardOfYesNo() {
...
JTextArea disclaimerTextArea = new JTextArea(_____________);
disclaimerTextArea.setLineWrap(true);
disclaimerTextArea.setWrapStyleWord(true);
disclaimerTextArea.setEditable(false);
_____________ scrollPane = _________________;
_____________ size = _______________;
scrollPane.___________________;
add(_________________);
...
Get hands-on with 1200+ tech skills courses.