JTabbedPane

class javax.swing.JTabbedPane

JTabbedPane is simply a stack of components in selectable layers. Each layer can contain one component which is normally a container. Tab extensions are used to move a given layer to the front of the tabbed pane view. These tab extensions are similar to labels in that they can have assigned text, an icon (as well as a disabled icon), background and foreground colors, and a tooltip.

To add a component to a tabbed pane, you use one of its overloaded add() methods. This creates a new selectable tab and reorganizes the other tab extensions, if necessary, so the new one will fit. You can also use the addTab() and insertTab() methods to create new selectable layers. The remove() method takes a component as a parameter and removes the tab associated with that component, if there is one.

Tab extensions can reside to the north, south, east, or west of the tabbed pane's content. The location is specified using its setTabPlacement() method and passing one of the corresponding SwingConstants fields as a parameter.

Vertical or horizontal tabs? When is it best to choose between vertical or horizontal tabs?

Three possible rules of thumb help make the decision whether to place tabs horizontally or vertically. First, consider the nature of the data to be displayed. Is vertical or horizontal space at a premium within the available display space? If, for example, you have a list with a single column but 200 entries, then clearly vertical space is at a premium. If you have a table with only 10 entries but 15 columns, then horizontal space is at a premium. Simply place the tabs where space is cheaper to obtain. In the first example with the long list, place the tabs vertically so they use horizontal space which is available. In the second example, place the tabs horizontally so you use vertical space which is available while horizontal space is completely taken by the table columns.

The second rule concerns the number and size of the tabs. If you need to display 12 tabs, for example, each with a long label, then it is unlikely that these will fit across the screen horizontally. In this case you are more likely to fit them by placing them vertically. Using space in these ways when introducing a tabbed pane should minimize the introduction of scroll panes and maximize ease of use. Finally, the third rule of thumb is to consider the layout and mouse movements required for operating the software. If, for example, your application uses a toolbar, then it may make sense to align the tabs close to the toolbar, thus minimizing mouse movements between the toolbar buttons and the tabs. If you have a horizontal toolbar across the top of the screen, then choose a horizontal set of tabs across the top (to the north).

JAVA 1.4 As of Java 1.4 you can choose whether tabs should wrap to form rows of tabs, or whether they should always form one scrollable row of column. When in the latter form two buttons appear for scrolling through the existing tabs.

The tab layout policy can be assigned with the new setTabLayoutPolicy() method. This method takes either of the following as a parameter:

JTabbedPane.WRAP_TAB_LAYOUT JTabbedPane.SCROLL_TAB_LAYOUT

Example 6.1, along with the corresponding figures, illustrates this new feature.

You can get and set the selected tab index at any given time using its getSelectedlndex () and setSelectedIndex() methods respectively. You can get/set the component associated with the selected tab similarly, using the getSelectedComponent() and setSelected-Component() methods.

One or more ChangeListeners can be added to a JTabbedPane, which gets registered with its model (an instance of DefaultSingleSelectionModel by default—see chapter 12 for more information about SingleSelectionModel and DefaultSingleSelectionModel). When a new tab is selected, the model will send out ChangeEvents to all registered ChangeListeners. The stateChanged () method of each listener is invoked, so you can capture and perform any desired actions when the user selects any tab. JTabbed-

Pane also fires PropertyChangeEvents whenever its model or tab placement properties change state.

GUIDELINE

Transaction boundaries and tabbed panes If you're using a tabbed pane within a dialog, the transaction boundary is normally clear—it will be an OK or Cancel button on the dialog. In this case, it is obvious that the OK and Cancel buttons would lie outside the tabbed pane and in the dialog itself. This is an important point. Place action buttons which terminate a transaction outside the tabbed panes. If, for example, you had a tabbed pane which contained a Save and Cancel button within the first tab, would it be clear that the Save and Cancel buttons work across all tabs or only on the first? Actually, it can be very ambiguous. To clearly define the transaction, define the buttons outside the tabbed pane so it is clear to the user that any changes made to any tab will be accepted or saved when OK or Save is pressed or discarded when Cancel is pressed. The action buttons will then apply across the complete set of tabs.

6.2 A dynamically changeable tabbed pane

We will now turn to a JTabbedPane example applet that demonstrates a dynamically reconfigurable tab layout as well as the addition and removal of any number of tabs. A Change-Listener is attached to the tabbed pane to listen for tab selection events and to display the currently selected tab index in a status bar. For enhanced feedback, audio clips are played when the tab layout changes and whenever a tab is added and removed. Example 6.1 contains the code.

0 0

Post a comment

  • Receive news updates via email from this site