Java JTabbedPane, how can I select a tab from a button?
Asked Answered
L

5

7

How can I select a tab as if it was clicked by clicking on a button? I have googled and looked at all the actions but there are just just so many... :(

Anyone know off hand?

Thanks in advance!

Lengthwise answered 27/5, 2009 at 17:7 Comment(0)
M
15

Add an action listener to the button that calls setSelectedComponent, or setSelectedIndex on the JTabbedPane.

Mathewson answered 27/5, 2009 at 17:44 Comment(0)
S
2

I'm not sure what you mean about the button, but you might be looking for setSelectedComponent or setSelectedIndex.

Steinke answered 27/5, 2009 at 17:14 Comment(0)
F
1

If your jtabbedpane's name is mytabbedpane it goes like this:

mytabbedpane.getSelectedIndex();

which returns the int of that tab (0,1 .. n) or

mytabbedpane.getSelectedComponent();

which returns the String of the tab's name ("Firts tab","Second tab",...).

If you want to use the "getSelectedComponent()" for boolean logic you should write something like:

if (mytabbedpane.getSelectedComponent().equals("First tab")) {
     //code here
}

and for the "getSelectedIndex()" one is of course:

if (mytabbedpane.getSelectedIndex() == 0) {
     //code here
}
Fifine answered 2/3, 2010 at 9:34 Comment(1)
.getSelectedComponent() returns Component, not String.Lakitalaks
F
1

Double click on button, put the follwing code

JTabbedPane.setSelectedIndex(1);

Tabs starts from 0 to N left to right order

Fostoria answered 23/10, 2018 at 18:35 Comment(0)
P
0

Try this code:

tabbedPane.addTab(tabName, component);
int count = tabbedPane.getTabCount();
tabbedPane.setSelectedIndex(count-1);
Philbin answered 16/5, 2013 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.