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.

  1. Remove the rows and columns parameter values from the call to the constructor for disclaimerTextArea.
  2. Remove the line of code that added the disclaimerTextArea to the bottom of the window.
  3. Create a new JScrollPane object called scrollPane, initialized by calling JScrollPane’s constructor with disclaimerTextArea as the parameter value.
  4. Create a new Dimension object called size that is 300 wide and 50 high.
  5. Set the preferred size of scrollPane to size using JScrollPane’s setPreferredSize() method.
  6. Add scrollPane to the bottom of the window using BorderLayout’s PAGE_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.