The Java Developers Almanac 1.4


Order this book from Amazon.

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

e832. Moving a Tab in a JTabbedPane Container

To move a tab, it must first be removed and then reinserted into the tabbed pane as a new tab. Unfortunately, since there is no object that represents a tab, it is necessary to record all of the tab's properties before moving it and to restore them after the new tab has been created.

This example moves the last tab to the first position:

    // To create a tabbed pane, see e828 Creating a JTabbedPane Container
    
    int src = pane.getTabCount()-1;
    int dst = 0;
    
    // Get all the properties
    Component comp = pane.getComponentAt(src);
    String label = pane.getTitleAt(src);
    Icon icon = pane.getIconAt(src);
    Icon iconDis = pane.getDisabledIconAt(src);
    String tooltip = pane.getToolTipTextAt(src);
    boolean enabled = pane.isEnabledAt(src);
    int keycode = pane.getMnemonicAt(src);
    int mnemonicLoc = pane.getDisplayedMnemonicIndexAt(src);
    Color fg = pane.getForegroundAt(src);
    Color bg = pane.getBackgroundAt(src);
    
    // Remove the tab
    pane.remove(src);
    
    // Add a new tab
    pane.insertTab(label, icon, comp, tooltip, dst);
    
    // Restore all properties
    pane.setDisabledIconAt(dst, iconDis);
    pane.setEnabledAt(dst, enabled);
    pane.setMnemonicAt(dst, keycode);
    pane.setDisplayedMnemonicIndexAt(dst, mnemonicLoc);
    pane.setForegroundAt(dst, fg);
    pane.setBackgroundAt(dst, bg);

 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
e833. Getting the Tabs 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.