![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Altering Existing ComponentsExisting components or tabs in a tabbed pane can be altered. To facilitate finding the index of a component, the indexOfComponent method is provided. Similarly, the indexOfTab method determines the index of a tab in the tabbed pane. These methods search the tabbed pane to determine the index of the given component, tag text, or icon. By using these methods, the application does not need to store the index of components or tabs in the tabbed pane. These methods are also useful to determine the index where a new component should be added. You can replace the tab at a given index with the setComponentAt method. This method takes the index of the component to be replaced and the component. The text or icon on the tab for the given index is not changed. Passing a negative index, or an index greater than the number of tabs in the view, will cause an ArrayIndexOutOfBoundsException to be thrown. You can query the component at the given index by using the getComponentAt method. The text or icon on an existing tab can be updated with the setTitleAt and setIconAt methods respectively. The current value for these properties can be queried with the getTitleAt and getIconAt methods. Once again, specifying an out of bounds index will cause an ArrayIndexOutOfBoundsException to be thrown. The setForegroundAt and setBackgroundAt methods set the foreground and background colors of the tabs at the given index. Note that these methods alter the colors in the tab but not the component itself. Thus, if you add the following lines of code to the JTabbedPaneTest application, the tabbed pane will look as shown in Figure 14.3. Notice that Tab 2 is selected in the figure. tabbedPane.setForegroundAt( 0, Color.red ); tabbedPane.setForegroundAt( 1, Color.yellow ); tabbedPane.setBackgroundAt( 0, Color.yellow ); tabbedPane.setBackgroundAt( 1, Color.red ;
You can query the bounds of a tab with the getBoundsAt method. This method returns a Rectangle containing the size of the tab at the given index. Once again, the size is of the tab itself, not the component in the tabbed pane. A tab can be enabled and disabled in the tabbed pane. The setEnableAt method changes the enabled state of a tab, and the isEnabledAt method queries the current state of this property. The setDisabledIconAt method sets an icon for a disable tab. If a disabled icon has not been set and the tab contains an icon, a disabled icon is created by using the GrayFilter class as shown in Chapter 5, Basic Components. The currently defined disabled icon for a tab can be queried with the getDisabledIconAt method. Displaying ComponentsThe JTabbedPane class employs a SingleSelectionModel instance to track which component is currently displayed in the container. The SingleSelectionModel interface, shown in Listing 14.2, defines a selection model that allows only one item to be selected at a given time. It contains methods for setting and querying the selected index, as well as clearing the selection. A ChangeListener can be added to the model to receive notifications when the selection changes. The ChangeListener interface and its associated ChangeEvent class are discussed in Chapter 6, The Button Hierarchy. It is interesting to note that the comment before the clearSlection method states that clearing the selection will set the selected value to 1. This is an implementation detail that should not be assumed by the user of the model. The DefaultSingleSelectionModel class in the JFC does exhibit this behavior, but other implementations of the model may not. The isSelected method is provided to determine if there is currently a selected index in the model. A ChangeListener can be added directly to the tabbed pane itself instead of with the model. The JTabbedPane class listens to the SingleSelectionModel and forwards change messages to all ChangeListeners registered with the JTabbedPane instance. Listing 14.2 The SingleSelectionModel Interface public interface SingleSelectionModel { /** * @return the models selection, or -1 if there is no selection. * @see #setSelectedIndex */ public int getSelectedIndex(); /** * Sets the models selected index to <I>index</I>. * * Notifies any listeners if the model changes * * @see #getSelectedIndex * @see #addChangeListener */ public void setSelectedIndex(int index); /** * Clears the selection (to -1). */ public void clearSelection(); /** * Returns true if the selection model currently has a selected value */ public boolean isSelected(); /** * Adds <I>listener</I> as a listener to changes in the model. */ void addChangeListener(ChangeListener listener); /** * Removes <I>listener</I> as a listener to changes in the model. */ void removeChangeListener(ChangeListener listener); } You can set the current SingleSelectionModel for the tabbed pane with the setModel method, and query it with the getModel method. Changing the selected index will cause listeners to be notified of the change. The JTabbedPane class contains the setSelectedIndex and getSelectedIndex convenience methods for setting and querying the selected index in the model. These methods simply call the equivalent methods on the current model. The getSelectedComponent method can be used to query the selected component. This method returns null if there is not a selected component. Similarly, you can set the displayed component by using the setSelectedComponent method.
|
![]() |
|