JTabbedPane: setMnemonicAt(int tabIndex, int mnemonic)
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
public static void main(String args[]) throws Exception {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
String titles[] = { "A", "B", "C", "D", "E", "F" };
int mnemonic[] = { KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_E,
KeyEvent.VK_F };
for (int i = 0, n = titles.length; i < n; i++) {
add(tabbedPane, titles[i], mnemonic[i]);
}
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
int index = sourceTabbedPane.getSelectedIndex();
System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index));
}
};
tabbedPane.addChangeListener(changeListener);
frame.add(tabbedPane, BorderLayout.CENTER);
frame.setSize(400, 150);
frame.setVisible(true);
}
static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
int count = tabbedPane.getTabCount();
JButton button = new JButton(label);
tabbedPane.addTab(label, button);
tabbedPane.setMnemonicAt(count, mnemonic);
}
}
Related examples in the same category
1. | JTabbedPane.SCROLL_TAB_LAYOUT | | |
2. | JTabbedPane: addChangeListener(ChangeListener l) | | |
3. | JTabbedPane: addTab(String title, Component component) | | |
4. | JTabbedPane: addTab(String title, Icon icon, Component component, String tip) | | |
5. | JTabbedPane: getComponentAt(int index) | | |
6. | JTabbedPane: getDisabledIconAt(int index) | | |
7. | JTabbedPane: getDisplayedMnemonicIndexAt(int tabIndex) | | |
8. | JTabbedPane: getIconAt(int index) | | |
9. | JTabbedPane: getMnemonicAt(int tabIndex) | | |
10. | JTabbedPane: getTitleAt(int index) | | |
11. | JTabbedPane: getToolTipTextAt(int index) | | |
12. | JTabbedPane: indexOfComponent(Component component) | | |
13. | JTabbedPane: indexOfTab(String title) | | |
14. | JTabbedPane: insertTab(String title, Icon icon, Component component, String tip, int index) | | |
15. | JTabbedPane: isEnabledAt(int index) | | |
16. | JTabbedPane: remove(Component component) | | |
17. | JTabbedPane: removeAll() | | |
18. | JTabbedPane: removeTabAt(int index) | | |
19. | JTabbedPane: setBackgroundAt(int index, Color background) | | |
20. | JTabbedPane: setDisabledIconAt(int index, Icon disabledIcon) | | |
21. | JTabbedPane: setDisplayedMnemonicIndexAt(int tabIndex, int mnemonicIndex) | | |
22. | JTabbedPane: setEnabledAt(int index, boolean enabled) | | |
23. | JTabbedPane: setForegroundAt(int index, Color foreground) | | |
24. | JTabbedPane: setSelectedIndex(int index) | | |
25. | JTabbedPane: setTabComponentAt(int index, Component component) | | |
26. | JTabbedPane: setTabLayoutPolicy(int tabLayoutPolicy) | | |
27. | JTabbedPane: setTabPlacement(int tabPlacement) | | |
28. | JTabbedPane: setToolTipTextAt(int index, String toolTipText) | | |