Scroll Panes

Components in a graphical user interface are often bigger than the area available to display them. To move from one part of the component to another, vertical and horizontal scrollbars are used—this is standard behavior for a text area in software such as word processors and email programs. In Swing, you offer scrolling by adding a component to a scroll pane, a container that is represented by the JScrollPane class in Swing. You can create a scroll pane with the following constructors:

  • JScrollPane()— Create a scroll pane with a horizontal and vertical scrollbar that appear, if they are needed
  • JScrollPane(int, int)— Create a scroll pane with the specified vertical scrollbar and horizontal scrollbars
  • JScrollPane(Component)— Create a scroll pane that contains the specified user interface component
  • JScrollPane(Component, int, int)— Create a scroll pane with the specified component, vertical scrollbar, and horizontal scrollbar

The integers used as arguments to these constructors determine how scrollbars will be used in the pane. Use the following class variables as these arguments:

  • JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED or JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
  • JScrollPane.VERTICAL_SCROLLBAR_NEVER or JScrollPane.HORIZONTAL_ SCROLLBAR_NEVER
  • JScrollPane.VERTICAL_SCROLLBAR_ALWAYS or JScrollPane.HORIZONTAL_ SCROLLBAR_ALWAYS

If you have created a scroll pane without a component in it, you can use the pane's add(Component) method to add components. After you have finished setting up a scroll pane, it should be added to a container in place of the component. To see an app that includes a scroll pane, enter Listing 16.1 into a text editor and save it as WriteMail.java.

Listing 16.1. The Full Text of WriteMail.java
 1: import javax.swing.*;
 2: import java.awt.*;
 3:
 4: public class WriteMail extends JFrame {
 5:
 6: public WriteMail() {
 7: super("Write an E-Mail");
 8: setSize(370, 270);
 9: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
10: FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);
11: setLayout(flow);
12:
13: JPanel row1 = new JPanel();
14: JLabel toLabel = new JLabel("To:");
15: row1.add(toLabel);
16: JTextField to = new JTextField(24);
17: row1.add(to);
18: add(row1);
19:
20: JPanel row2 = new JPanel();
21: JLabel subjectLabel = new JLabel("Subject:");
22: row2.add(subjectLabel);
23: JTextField subject = new JTextField(24);
24: row2.add(subject);
25: add(row2);
26:
27: JPanel row3 = new JPanel();
28: JLabel messageLabel = new JLabel("Message:");
29: row3.add(messageLabel);
30: JTextArea message = new JTextArea(4, 22);
31: message.setLineWrap(true);
32: message.setWrapStyleWord(true);
33: JScrollPane scroll = new JScrollPane(message,
34: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
35: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
36: row3.add(scroll);
37: add(row3);
38:
39: JPanel row4 = new JPanel();
40: JButton send = new JButton("Send");
41: row4.add(send);
42: add(row4);
43:
44: setVisible(true);
45: }
46:
47: public static void main(String[] arguments) {
48: WriteMail mail = new WriteMail();
49: }
50: }


After you compile and run the app, you should see a window like the one in Screenshot.

Screenshot Displaying a scrolling text area in an app.

Java ScreenShot


The WriteMail app is a graphical user interface that is used to compose an email. There is no event-handling code in the program, so you can't do anything with the data entered in the form. The text of an email is entered in a scrolling text area, which is implemented with the following statements:

JTextArea message = new JTextArea(4, 22);
message.setLineWrap(true);
message.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(message,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
row3.add(scroll);


      
Comments