Skip to main content

skip to main content

developerWorks  >  Java technology  >

Magic with Merlin: Scrolling tabbed panes

What to do when it's just too big to fit

developerWorks
Document options

Document options requiring JavaScript are not displayed

Discuss

Sample code


Rate this page

Help us improve this content


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
Wide Single Line JTabbedPane

Figure 2. Narrow, multiline JTabbedPane
Narrow Multiline JTabbedPane


Back to top


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
Narrow Multiline 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
JTabbedPane with Left Tab Placement


Back to top


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();
  }
}




Back to top


Download

DescriptionNameSizeDownload method
Code samplej-mer0905Tabs.zip1 KBHTTP
Information about download methods


Resources



About the author

Author photo

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.




Rate this page


Please take a moment to complete this form to help us better serve you.



YesNoDon't know
 


 


12345
Not
useful
Extremely
useful
 


Back to top