The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JTabbedPane  [13 examples]

e833. Getting the Tabs in a JTabbedPane Container

This example retrieves all the tabs in a tabbed pane:
    // To create a tabbed pane, see e828 Creating a JTabbedPane Container
    
    // Get number of tabs
    int count = pane.getTabCount();
    
    // Get the properties of each tab
    for (int i=0; i<count; i++) {
        // Get label
        String label = pane.getTitleAt(i);
    
        // Get icon
        Icon icon = pane.getIconAt(i);
    
        // Get tool tip
        String tooltip = pane.getToolTipTextAt(i);
    
        // Is enabled?
        boolean enabled = pane.isEnabledAt(i);
    
        // Get mnemonic
        int keycode = pane.getMnemonicAt(i);
    
        // Get component associated with tab
        Component comp = pane.getComponentAt(i);
    }
Most of the methods that allow the properties of a tab to be changed require the index of the tab. The index of a tab can change as tabs are added, removed, or moved. Here are three ways to retrieve the index of a tab when needed.
    // Get the index of the first tab that matches a label
    String label = "Tab Label";
    int index = pane.indexOfTab(label);
    
    // Get the index of the first tab that matches an icon; the supplied
    // icon must be the same instance that was used to create the tab
    index = pane.indexOfTab(icon);
    
    // Get the index of the tab by matching the child component; the supplied
    // component must be the same instance that was used to create the tab
    index = pane.indexOfComponent(component);
    
    
    if (index < 0) {
        // The tab could not be found
    }

 Related Examples
e828. Creating a JTabbedPane Container
e829. Getting and Setting the Selected Tab in a JTabbedPane Container
e830. Adding a Tab to a JTabbedPane Container
e831. Removing a Tab in a JTabbedPane Container
e832. Moving a Tab in a JTabbedPane Container
e834. Setting the Location of the Tabs in a JTabbedPane Container
e835. Enabling and Disabling a Tab in a JTabbedPane Container
e836. Setting the Tool Tip for a Tab in a JTabbedPane Container
e837. Setting the Color of a Tab in a JTabbedPane Container
e838. Enabling the Selection of a Tab in a JTabbedPane Container Using a Keystroke
e839. Enable Scrolling Tabs in a JTabbedPane Container
e840. Determining When the Selected Tab Changes in a JTabbedPane Container

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JRadioButton    JScrollPane    JSlider    JSpinner    JSplitPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.