JTabbedPane

A JTabbedPane arranges GUI components into layers in which only one layer is visible at a time. Users access each layer via a tabsimilar to folders in a file cabinet. When the user clicks a tab, the appropriate layer is displayed. The tabs appear at the top by default but also can be positioned at the left, right or bottom of the JTabbedPane. Any component can be placed on a tab. If the component is a container, such as a panel, it can use any layout manager to lay out several components on the tab. Class JTabbedPane is a subclass of JComponent. The application in Fig. 22.13 and Fig. 22.14 creates one tabbed pane with three tabs. Each tab displays one of the JPanelspanel1, panel2 or panel3.

Figure 22.13. JTabbedPane used to organize GUI components.

 1 // Fig. 22.13: JTabbedPaneFrame.java
 2 // Demonstrating JTabbedPane.
 3 import java.awt.BorderLayout;
 4 import java.awt.Color;
 5 import javax.swing.JFrame;
 6 import javax.swing.JTabbedPane;
 7 import javax.swing.JLabel;
 8 import javax.swing.JPanel;
 9 import javax.swing.JButton;
10 import javax.swing.SwingConstants;
11
12 public class JTabbedPaneFrame extends JFrame
13 {
14 // set up GUI
15 public JTabbedPaneFrame()
16 {
17 super( "JTabbedPane Demo " );
18
19 JTabbedPane tabbedPane = new JTabbedPane(); // create JTabbedPane
20
21 // set up pane11 and add it to JTabbedPane
22 JLabel label1 = new JLabel( "panel one", SwingConstants.CENTER );
23 JPanel panel1 = new JPanel(); // create first panel
24 panel1.add( label1 ); // add label to panel
25 tabbedPane.addTab( "Tab One", null, panel1, "First Panel" );
26
27 // set up panel2 and add it to JTabbedPane
28 JLabel label2 = new JLabel( "panel two", SwingConstants.CENTER );
29 JPanel panel2 = new JPanel(); // create second panel
30 panel2.setBackground( Color.YELLOW ); // set background to yellow
31 panel2.add( label2 ); // add label to panel
32 tabbedPane.addTab( "Tab Two", null, panel2, "Second Panel" );
33
34 // set up panel3 and add it to JTabbedPane
35 JLabel label3 = new JLabel( "panel three" );
36 JPanel panel3 = new JPanel(); // create third panel
37 panel3.setLayout( new BorderLayout() ); // use borderlayout
38 panel3.add( new JButton( "North" ), BorderLayout.NORTH );
39 panel3.add( new JButton( "West" ), BorderLayout.WEST );
40 panel3.add( new JButton( "East" ), BorderLayout.EAST );
41 panel3.add( new JButton( "South" ), BorderLayout.SOUTH );
42 panel3.add( label3, BorderLayout.CENTER );
43 tabbedPane.addTab( "Tab Three", null, panel3, "Third Panel" );
44
45 add( tabbedPane ); // add JTabbedPane to frame
46 } // end JTabbedPaneFrame constructor
47 } // end class JTabbedPaneFrame

Figure 22.14. Test class for JTabbedPaneFrame.

 1 // Fig. 22.14: JTabbedPaneDemo.java
 2 // Demonstrating JTabbedPane.
 3 import javax.swing.JFrame;
 4
 5 public class JTabbedPaneDemo
 6 {
 7 public static void main( String args[] )
 8 {
 9 JTabbedPaneFrame tabbedPaneFrame = new JTabbedPaneFrame();
10 tabbedPaneFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
11 tabbedPaneFrame.setSize( 250, 200 ); // set frame size
12 tabbedPaneFrame.setVisible( true ); // display frame
13 } // end main
14 } // end class JTabbedPaneDemo
 

The constructor (lines 1546) builds the GUI. Line 19 creates an empty JTabbedPane with default settingsthat is, tabs across the top. If the tabs do not fit on one line, they will wrap to form additional lines of tabs. Next the constructor creates the JPanels panel1, panel2 and panel3 and their GUI components. As we set up each panel, we add it to tabbedPane, using JTabbedPane method addTab with four arguments. The first argument is a string that specifies the title of the tab. The second argument is an Icon reference that specifies an icon to display on the tab. If the Icon is a null reference, no image is displayed. The third argument is a Component reference that represents the GUI component to display when the user clicks the tab. The last argument is a string that specifies the tool tip for the tab. For example, line 25 adds JPanel panel1 to tabbedPane with title "Tab One" and the tool tip "First Panel". JPanels panel2 and panel3 are added to tabbedPane at lines 32 and 43. To view a tab, click it with the mouse or use the arrow keys to cycle through the tab=.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net