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

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

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();
}
}
|
| Description | Name | Size | Download method |
|---|---|---|---|
| Code sample | j-mer0905Tabs.zip | 1 KB | HTTP |
Information about download methods
- Learn more about the changes to the Swing Component Set in the 1.4 release.
- Learn more about the
JTabbedPaneby reading its javadoc. - Try out a TabSplitter component with earlier versions of the Java language.
- Read the complete collection of Merlin tips by John Zukowski.
- Find more resources on the developerWorks Java technology zone.

John Zukowski conducts strategic Java consulting with JZ Ventures, Inc. and serves as the resident guru for a number of jGuru's community-driven Java FAQs. His latest books are Java Collections and Definitive Guide to Swing for Java 2 (2nd ed) from Apress. Contact John at jaz@zukowski.net.





