CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Layout

Controls and layout managers
Components
Scroll panes

A frame that displays a text area in a scroll pane

Common constructors of the JScrollPane class

Constructor Description
JScrollPane(Component) Creates a scroll pane that displays the specified component, along with vertical and horizontal scrollbars as needed.
JScrollPane(Component, vertical, horizontal) Creates a scroll pane that displays the specified component and uses the specified vertical and horizontal policies.

Fields of the ScrollPaneConstants interface that set scrollbar policies

Field Description
VERTICAL_SCROLLBAR_ALWAYS Always display a vertical scrollbar.
VERTICAL_SCROLLBAR_AS_NEEDED Display a vertical scrollbar only when needed.
VERTICAL_SCROLLBAR_NEVER Never display a vertical scrollbar.
HORIZONTAL_SCROLLBAR_ALWAYS Always display a horizontal scrollbar.
HORIZONTAL_SCROLLBAR_AS_NEEDED Display a horizontal scrollbar only when needed.
HORIZONTAL_SCROLLBAR_NEVER Never display a horizontal scrollbar.

Code that uses a scroll pane with a text area

private JTextArea commentTextArea = new JTextArea(7, 20);
commentTextArea.setLineWrap(true);
commentTextArea.setWrapStyleWord(true);
JScrollPane commentScroll = new JScrollPane(commentTextArea);
add(commentScroll);

Code that creates a scroll pane and sets the scroll bar policies

JScrollPane commentScroll =
    new JScrollPane(commentTextArea,
        ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
Previous | Summary | Text areas | Scroll panes | Check boxes | Radio buttons | Borders | Combo boxes | Event listeners | Lists | Multiple selections in a list | List models | Next