Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

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:

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:

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 application that includes a scroll pane, enter Listing 16.1 into a text editor and save it as WriteMail.java.

Example 16.1. The Full Text of WriteMail.java

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

After you compile and run the application, you should see a window like the one in Figure 16.1.

16fig01.gif

Figure 16.1 Displaying a scrolling text area in an application.

The WriteMail application 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); 

Share ThisShare This

Informit Network