Skip to main content

Magic with Merlin: Scrolling tabbed panes

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

John Zukowski (jaz@zukowski.net), President, JZ Ventures, Inc.
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.

Summary:  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.

View more content in this series

Date:  01 Sep 2001
Level:  Introductory
Activity:  1876 views

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

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

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

DescriptionNameSizeDownload method
Code samplej-mer0905Tabs.zip1 KB HTTP

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.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=10583
ArticleTitle=Magic with Merlin: Scrolling tabbed panes
publish-date=09012001
author1-email=jaz@zukowski.net
author1-email-cc=jaloi@us.ibm.com

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers