Changing tab's title, icon, mnemonic, tooltip, or component on a particular tab with one of the setXXXAt() methods : JTabbedPane « Swing « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Swing » JTabbedPane 
14.51.4.Changing tab's title, icon, mnemonic, tooltip, or component on a particular tab with one of the setXXXAt() methodsPrevious/Next

The displayed mnemonic index refers to which time a particular character in the title should be highlighted. In addition, you can change the background or foreground of a specific tab, enable or disable a specific tab, or have a different disabled icon with additional setXXXAt() methods:

  1. public void setTitleAt(int index, String title)
  2. public void setIconAt(int index, Icon icon)
  3. public void setMnemonicAt(int index, int mnemonic)
  4. public void setDisplayedMnemonicIndexAt(int index, int mnemonicIndex)
  5. public void setToolTipTextAt(int index, String text)
  6. public void setComponentAt(int index, Component component)
Changing tab's title, icon, mnemonic, tooltip, or component  on a particular tab with one of the setXXXAt() methods
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;

public class TabSample {
  static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label);
    tabbedPane.setMnemonicAt(count, mnemonic);
  }

  public static void main(String args[]) {

    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane();

    String titles[] "General""Security""Content""Connection""Programs""Advanced" };
    int mnemonic[] KeyEvent.VK_G, KeyEvent.VK_S, KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_P,
        KeyEvent.VK_A };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i], mnemonic[i]);
    }

    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400150);
    frame.setVisible(true);
  }
}
14.51.JTabbedPane
14.51.1.First usage of JTabbedPane
14.51.2.Adding and Removing TabsAdding and Removing Tabs
14.51.3.Get the index of the first tab that matches an icon
14.51.4.Changing tab's title, icon, mnemonic, tooltip, or component on a particular tab with one of the setXXXAt() methodsChanging tab's title, icon, mnemonic, tooltip, or component  on a particular tab with one of the setXXXAt() methods
14.51.5.Adds tool tips to a table using a rendererAdds tool tips to a table using a renderer
14.51.6.Specifying a tab's location: TOP, BOTTOM, LEFT, or RIGHTSpecifying a tab's location: TOP, BOTTOM, LEFT, or RIGHT
14.51.7.Changing background, foreground and iconChanging background, foreground and icon
14.51.8.To remove a tab, you can remove a specific tab with removeTabAt(int index), remove(int index), or remove(Component component)To remove a tab, you can remove a specific tab with  removeTabAt(int index), remove(int index), or remove(Component component)
14.51.9.Listening for Selected Tab ChangesListening for Selected Tab Changes
14.51.10.TabLayout Policy: SCROLL_TAB_LAYOUT or WRAP_TAP_LAYOUTTabLayout Policy: SCROLL_TAB_LAYOUT or WRAP_TAP_LAYOUT
14.51.11.Add Components to JTabbedPane
14.51.12.New Methods in the JTabPane Component (Add component to JTabPane)
14.51.13.JTabPane with TextField in the tabJTabPane with TextField in the tab
14.51.14.Add user icon to tab panelAdd user icon to tab panel
14.51.15.Add Button to tab barAdd Button to tab bar
14.51.16.Add a tab with a label taken from the name of the component
14.51.17.Add a tab with a label at the end of all tabs
14.51.18.Add a tab with a label and icon at the end of all tabs
14.51.19.Add a tab with a label, icon, and tool tip at the end of all tabs
14.51.20.Getting the Tabs in a JTabbedPane Container
14.51.21.Enable Scrolling Tabs in a JTabbedPane Container
14.51.22.Enabling the Selection of a Tab in a JTabbedPane Container Using a Keystroke
14.51.23.Enabling and Disabling a Tab in a JTabbedPane Container
14.51.24.Setting the Color of a Tab in a JTabbedPane Container
14.51.25.Setting the Tool Tip for a Tab in a JTabbedPane Container
14.51.26.Setting the Location of the Tabs in a JTabbedPane Container
14.51.27.Determining When the Selected Tab Changes in a JTabbedPane Container
14.51.28.Moving a Tab in a JTabbedPane Container
14.51.29.Insert a tab after the first tab
14.51.30.Get the index of the tab by matching the child component
14.51.31.Getting and Setting the Selected Tab in a JTabbedPane Container
14.51.32.Setting the Size of the Divider in a JSplitPane Container
14.51.33.Customizing a JTabbedPane Look and Feel
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.