Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
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 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.
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);
Sliders | Next Section

Account Sign In
View your cart