 | Level: Introductory John Zukowski (jaz@zukowski.net), President, JZ Ventures, Inc.
01 Sep 2001 Prior to the Merlin release of the Java platform, Swing's JTabbedPane control only allowed you to wrap tabs across multiple rows. The new and improved version of JTabbedPane lets you scroll the tabs across a single row. In this installment of Magic with Merlin, John Zukowski demonstrates how to change the layout policy of JTabbedPane and provides a complete example for you to use. Getting started
The JTabbedPane control is a container that allows a user to access different component sets based on which tab he or she selects. When you're designing a user interface with JTabbedPane, it's not uncommon to specify many tabs. A problem arises if the user's screen size is smaller than expected; in this case, the tabs will wrap across multiple rows. Wrapping causes the expected screen space for the components on each tab to decrease. The area decreases proportionally to the number of rows needed to display the tabs across the JTabbedPane -- a user interface engineer's nightmare.
Figure 1 illustrates how a screen might look as originally set by the UI engineer, and Figure 2 shows what will happen with a smaller-than-expected screen width. Figure 1. Wide single-line JTabbedPane

Figure 2. Narrow, multiline JTabbedPane

Changing layout policy
The Merlin release allows you to change this behavior by setting the new tabLayoutPolicy property of JTabbedPane. By changing the default setting from JTabbedPane.WRAP_TAB_LAYOUT to JTabbedPane.SCROLL_TAB_LAYOUT the tabs will no longer wrap, instead staying one row, as shown in Listing 1. Arrows are added on the end for moving to tabs that don't fit on the screen, as shown in Figure 3.
JTabbedPane pane = new JTabbedPane();
pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
|
Figure 3. Narrow, single-line JTabbedPane

The layout policy is also helpful if the tabs are laid out vertically (see Figure 4), instead of horizontally. Figure 4. JTabbedPane with left tab placement

A complete example
That's really all there is to it. Listing 2 contains the complete example source, which you can download in Download. Listing 2. Complete example
import javax.swing.*;
import java.text.*;
import java.awt.*;
public class Tabs {
public static void main(String args[]) {
String[] months = new DateFormatSymbols().getShortMonths();
JTabbedPane pane = new JTabbedPane();
pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
// pane.setTabPlacement(JTabbedPane.LEFT);
for (int i=0; i < 12; i++) {
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton(months[i]);
panel.add(button);
pane.add(months[i], panel);
}
JFrame frame = new JFrame("Tabs");
frame.getContentPane().add(pane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.show();
}
}
|
Download | Description | Name | Size | Download method |
|---|
| Code sample | j-mer0905Tabs.zip | 1 KB | HTTP |
|---|
Resources
About the author
Rate this page
|  |