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);