Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 300
Relequestual Avatar asked May 27 '09 17:05

Relequestual


3 Answers

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

like image 110
Yishai Avatar answered Oct 05 '22 12:10

Yishai


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

like image 44
Michael Myers Avatar answered Oct 05 '22 13:10

Michael Myers


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
}
like image 39
Dimitrios K. Avatar answered Oct 05 '22 14:10

Dimitrios K.