简体   繁体   中英

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!

向调用setSelectedComponent的按钮添加动作侦听器,或者在JTabbedPane上调用setSelectedIndex。

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
}

Double click on button, put the follwing code

JTabbedPane.setSelectedIndex(1);

Tabs starts from 0 to N left to right order

我不确定你对这个按钮的意思,但你可能正在寻找setSelectedComponentsetSelectedIndex

Try this code:

tabbedPane.addTab(tabName, component);
int count = tabbedPane.getTabCount();
tabbedPane.setSelectedIndex(count-1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM