Java Utililty Methods JTabbedPane

List of utility methods to do JTabbedPane

Description

The list of methods to do JTabbedPane are organized into topic(s).

Method

voidinstallPrefsHandler(final Preferences prefs, final String name, final JTabbedPane tabbedPane)
Installs a JTabbedPane preferences handler.
int selectedTab = prefs.getInt(name, 0);
try {
    tabbedPane.setSelectedIndex(selectedTab);
} catch (IndexOutOfBoundsException e) {
tabbedPane.addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent e) {
...
voidinstallPrefsHandler(Preferences prefs, String string, JTabbedPane tabbedPane)
install Prefs Handler
throw new UnsupportedOperationException("Not yet implemented");
booleanisInTabbedContainer(Component c)
is In Tabbed Container
Component parent = c.getParent();
while (null != parent) {
    if (parent instanceof JComponent && "TabbedContainerUI".equals(((JComponent) parent).getUIClassID())) 
        return true;
    parent = parent.getParent();
return false;
voidmoveTab(JTabbedPane tabPane, int srcIndex, int dstIndex)
As far as internal structure of JTabbedPane data model does not correspond to its visual representation, that is, component located on tab with index X is not located in the internal components array using the same index, we have to rearrange this array each time tab has been moved and repopulate/repaint JTabbedPane instance after that.
int cnt = tabPane.getTabCount();
Component[] components = new Component[cnt];
for (int i = 0; i < cnt; i++) {
    components[i] = tabPane.getComponent(i);
String[] captions = new String[cnt];
for (int i = 0; i < cnt; i++) {
    captions[i] = tabPane.getTitleAt(i);
...
voidselectTab(JTabbedPane pane, String title)
Selects the tab with the specified title.
int count = pane.getTabCount();
for (int i = 0; i < count; i++) {
    if (pane.getTitleAt(i).equals(title)) {
        pane.setSelectedIndex(i);
        break;
voidsetDefaultMnemonics(final JTabbedPane jtp)
set Default Mnemonics
for (int i = 0; i < jtp.getTabCount(); i++) {
    switch (i) {
    case 0:
        jtp.setMnemonicAt(i, KeyEvent.VK_1);
        break;
    case 1:
        jtp.setMnemonicAt(i, KeyEvent.VK_2);
        break;
...
voidsetSelectComponentTab(Component component)
set Select Component Tab
Component parent = component.getParent();
while (parent != null) {
    if (parent instanceof JTabbedPane) {
        JTabbedPane tabbedPane = (JTabbedPane) parent;
        tabbedPane.setSelectedComponent(component);
        break;
    component = parent;
...
voiduseTabToChangeFocus(Component comp)
use Tab To Change Focus
Set<KeyStroke> strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke("pressed TAB")));
comp.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, strokes);
strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke("shift pressed TAB")));
comp.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, strokes);