Chapter 14 JTabbedPane Class
- In This Chapter
- JTabbedPane Class Usage
The JTabbedPane class provides a visual component that allows multiple panels to be viewed one at a time in a single area. Tabs are provided to change from panel to panel in the main view. Tabbed panels are often used in configuration dialog boxes. Related options are placed on the same panel. The tabs on the tabbed panel represent the complete set of options available for configuration. This allows a great deal of information to be presented to the user in a relatively small amount of screen real estate. Using tabs to switch from panel to panel utilizes the screen space efficiently.
In this chapter you will learn how to
- Create and configure JTabbedPane class instances
- Add and remove panels from instances of the JTabbedPane class
- Dynamically alter the component and tabs title and icon
- Programmatically alter the panel displayed in the tabbed pane
JTabbedPane Class Usage
The JTabbedPane class is a container that allows multiple components to be added but only displays one component at a time. Tabs are located on one of the sides of the displayed component that allow the user to choose which component is currently visible in the center of the tabbed pane.
Creating an instance of the JTabbedPane class is a simple matter. The class contains two constructors: an empty constructor and one that takes a single parameter which specifies where the tabs are located on the panel. You can position the tabs on any side of the tabbed pane. The parameters to define the placement, defined in the SwingConstants class, are TOP, BOTTOM, RIGHT, and LEFT. The JTabbedPaneTest application shown in Listing 14.1 creates a tabbed pane with the tabs on the bottom and displays it in a frame. When using the default constructor, the tabs are placed on the top of the tabbed pane.
Tab placement is a bound property that can be changed after creation. The setTabPlacement method sets the property and invalidates the tabbed panel. The property name is tabPlacement. See Table 14.1 for a complete list of bound properties defined in the JTabbedPane class. You can query the current value of the property by using the provided getTabPlacement method. The number of tabs in a tabbed pane can be queried with the getTabCount method, and the number of rows required to display the tabs can be queried with the getTabRunCount method.
Listing 14.1 JTabbedPaneTest Application
package com.foley.test;
import java.awt.*;
import javax.swing.*;
import com.foley.utility.ApplicationFrame;
/**
* An application that displays a JTabbedPane in a frame.
*
* @author Mike Foley
**/
public class JTabbedPaneTest extends Object {
/**
* Application entry point.
* Create the frame, and display a tabbed pane in it.
*
* @param args Command line parameter. Not used.
**/
public static void main( String args[] ) {
JFrame frame = new ApplicationFrame( JTabbedPaneTest );
JTabbedPane tabbedPane = new JTabbedPane( SwingConstants.BOTTOM );
tabbedPane.setBorder( BorderFactory.createLoweredBevelBorder() );
frame.getContentPane().add( tabbedPane, BorderLayout.CENTER );
frame.pack();
frame.setVisible( true );
} // main
} // JtabbedPaneTest
Table 14.1 New Bound Properties in the JTabbedPane Class
|
Property Name
| Setter Method
| Getter Method
|
|
model
| setModel
| getModel
|
tabPlacement
| setTabPlacement
| getTabPlacement
|
|
|