我的问题是我需要将被单击的选项卡设置为JTabbedPane中最左侧的选项卡。我需要使用什么方法来完成此任务?
最佳答案
您需要添加一个ChangeListener,以便知道何时选择了选项卡。然后,您可以使用JTabbedPane中的方法删除并重新插入特定的索引。
tabbedPane.addChangeListener(new ChangeListener() {
// you need this so you can ignore ChangeEvents as you're removing & inserting panes
boolean listening = true;
@Override
public void stateChanged(ChangeEvent e)
{
int index = tabbedPane.getSelectedIndex();
if (listening && index != 0)
{
listening = false;
// get whatever info you need to recreate the tab
String title = tabbedPane.getTitleAt(index);
Component component = tabbedPane.getTabComponentAt(index);
// remove the old tab
tabbedPane.removeTabAt(index);
// insert the new one in the correct place
tabbedPane.insertTab(title, null, component, null, 0);
// select the current tab
tabbedPane.setSelectedIndex(0);
listening = true;
}
}
});