Brought to you by EarthWeb
ITKnowledge Logo Login Graphic IBM DB2 Universal Database.
IBM DB2 Universal Database.
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Adding Components to the JTabbedPane Instance

The frame created by the JTabbedPaneTest application is shown in Figure 14.1. In the figure, you see that an empty tabbed pane is not very interesting. Components must be added to the tabbed pane for the tabs to display. The original API for the JTabbedPane class contained many versions of the addTab and insertTab methods for adding components to the tabbed pane. However, these method names do not follow the JavaBeans standard naming convention for adding components to a container. This caused visual design tools difficulty, because the JTabbedPane class had to be handled with special code. To address this issue, a set of overloaded add methods were added to the JTabbedPane class that map to the appropriate addTab or insertTab method. The add method appends the given component to the existing set of components in the tabbed panel. Using the add method, the tabs will be displayed in the order the components are added to the tabbed pane. When a component is added by using the insertTab method, its placement can be specified. These methods are consistent to the methods you saw in earlier chapters for adding menu items to menus and items to a toolbar.


Figure 14.1  The JTabbedPaneTest application.

Use the following code to add a component to a tabbed pane:

tabbedPane.add( new JLabel( “Tab 1” ) );

This version of the add method uses the name of the component for the tab’s label in the tabbed pane. It is often desirable to specify the text for the tab’s label when adding the component to the tabbed pane. Due to a quirk in the various add method implementations, both of the following lines perform this task:

tabbedPane.add( “Tab 1”, new JLabel( “Tab 1” ) );
tabbedPane.add( new JLabel( “Tab 2” ), “Tab 2” );

This seemingly contradictory code is because the version of the add method that takes an Object constraint as its second parameter tests to see if the Object is an instance of the String class. If so, the add method calls the addTab method with the String constraint used as the tab’s name. There is also an add method that takes the position of where the new tab is located. To add the new tab at the start of the tabs, the following line of code can be used. This version of the add method calls the insert method with the specified index.

tabbedPane.add( new JLabel( “Tab 3” ), “Tab 3 “, 0 );

As was mentioned earlier, each version of the add method calls either the addTab or insertTab method contained in the JTabbedPane class. In fact, each variant of the addTab method calls the insertTab method. The signatures for these methods are shown next. Looking at the signature, you see that an Icon can be added to the tab for a component. The tab can contain an Icon, a String, or both. Also, the ToolTip for a tab can be specified when adding a component to the tabbed pane. However, there is no convenient method to alter the ToolTip after the component has been added to the tabbed pane.

addTab( String title, Icon icon, Component component, String tip )
addTab( String title, Icon icon, Component component )
addTab( String title, Component component )
insertTab( String title, Icon icon, Component component,
   String tip, int index )

Adding Tabs to the JTabbedPaneTest Application

You can use any or all of the previously shown methods to add tabs to the JTabbedPaneTest application. If the following lines of code are added after the tabbed pane is created, the window shown in Figure 14.2 will be created:

//
// add some tabs to the tabbed pane instance.
//
tabbedPane.add( new JLabel( “Tab 1” ) );
tabbedPane.add( new JLabel( “Tab 2” ), “Tab 2” );
tabbedPane.add( new JLabel( “Tab 3” ), “Tab 3 “, 0 );
tabbedPane.addTab( “Tab 4”, new ImageIcon( “red.gif” ),
                   new JLabel( “Tab 4” ), “This is the tip” );


Figure 14.2  Tabs added to the JTabbedPaneTest application.

With the exception of the tab labeled Tab 3, the order of the tabs in Figure 14.2 are the order in which they were added to the tabbed pane. The version of the add method used for this tab specified an index of 0 placing that tab first in the tabbed panel. The last tab is the only tab for which a ToolTip and an icon have been specified.

Removing Components from the JTabbedPane Instance

After adding components to an instance of the JTabbedPane class, it may become necessary to remove one or more of the components. The removeTabAt method removes the tab at the given index from the tabbed pane. Indexes in the tabbed pane are zero-based with a range from 0 to one less than the integer returned from the getTabCount method. This method requires that the numerical index of the component be removed. If you have a reference to the component to be removed, the remove method can be called. This method takes the component as its parameter.

You can clear the tabbed pane by calling the removeAll method. This method will clear the tabbed pane and leave it in the same state it was after being created.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.