Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

CloseAndMaxTabbedPane: An enhanced JTabbedPane

Add Close and Maximize buttons to your tabbed pane

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Professional Java Swing-based applications implement custom Swing components to enhance their functionality and usability. For example, IDEs like Eclipse and NetBeans use an enhanced tabbed pane as a container for source code editors. This tabbed pane has a Close icon on each tab that lets you easily close individual tabs.

In this article, we present the implementation of CloseAndMaxTabbedPane, an enhanced tabbed pane with Close and Maximize buttons. The component's look and feel is similar to Eclipse 2.1. You can also selectively enable/disable the Close or Maximize buttons as necessary. Figure 1 illustrates the screenshot of CloseAndMaxTabbedPane.

Figure 1. CloseAndMaxTabbedPane in action. Click on thumbnail to view full-size image.

Implement CloseAndMaxTabbedPane

The UML diagram in Figure 2 shows the implementation's most critical classes and how they relate to standard Java Swing classes.

Figure 2. High-level UML diagram of CloseAndMaxTabbedPane

Descriptions of the component's critical classes follow:

  1. CloseTabbedPaneUI (extends javax.swing.BasicTabbedPaneUI): This class is the new component's UI delegate object and is responsible for the tabbed pane's graphical aspects (including painting the Close and Maximize buttons). This class also fires events to the tabbed pane itself based on user actions and tries to be friendly with the application's current look and feel.
  2. CloseTabbedPaneEnhancedUI (extends CloseTabbedPaneUI): This class implements a different look and feel similar to the tabbed pane found in Eclipse 2.1. It overrides a set of paint methods to implement the enhanced look and feel.
  3. CloseAndMaxTabbedPane (extends javax.swing.JTabbedPane): This class extends the Java standard JTabbedPane and uses the CloseTabbedPaneUI. It also implements methods to add specific listeners (to Close and Maximize buttons) to the tabbed pane and interact with the UI.


Add buttons to the tabbed pane

It would be easy to add buttons if each tab were a separate Swing component. Ideally, then you would add a Close button by simply doing the following (forgetting layout aspects for the moment):

JButton closeButon = new JButton(closeButtonIcon); 
tab.add(closeButton);


And then adding an action listener by:

closeButton.addActionListener(someActionListener);


Unfortunately adding buttons is not that easy. Each tab in a tabbed pane is not a Swing component. Each tab's header (border, title, icon) is painted by the BasicTabbedPaneUI class. To paint our own version of the tabbed pane (i.e., to add the little buttons in the corner of each tab), we need to extend this class and override the paint methods to actually draw the buttons. Please note that the buttons are just painted graphics and not standard Java JButton components.

Interact with the pseudo buttons

Because there are no real buttons, we can't add action listeners to them. The only way to interact with these painted buttons is to add a mouse listener to the tabbed pane itself (or to one of its components). This mouse listener can then track mouse clicks or movements in the zone where the buttons are painted and fire specific events to the tabbed pane.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (2)
Login
Forgot your account info?

Platform dependent codeBy Anonymous on October 15, 2009, 2:45 pmThe article forgets to mention one of the greatest drawbacks of this code in its list of limitations, that it is strongly tied to Windows. Swing is used when one...

Reply | Read entire comment

testedBy Anonymous on November 10, 2008, 12:32 pmFirst I try to run a code. It does not working. not checked why. http://java.sun.com/docs/books/tutorial/uiswing/components/tabbedpane.html this work but only...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources