Java重新排序JTabbedPane中的选项卡

时间:2015-03-17 19:23:56

标签: java jtable jtabbedpane

我的问题是我需要设置被点击的标签,成为JTabbedPane中最左边的标签。我需要用什么方法来实现这个目标?

2 个答案:

答案 0 :(得分:1)

您需要添加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;
            }
        }
    });

答案 1 :(得分:0)

尝试以下JTabbedPane方法:

// get tab at mouse position
indexAtLocation(int x, int y)
// or get the selected tab
int getSelectedIndex()
// remove the tab at the determined index ...
removeTabAt(int index)
// ... and add it at a new one
insertTab(String title, Icon icon, Component component, String tip, int index)