簡體   English   中英

JTabbedPane中的Java重新排序選項卡

[英]Java reorder tabs in JTabbedPane

我的問題是我需要將單擊的選項卡設置為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;
            }
        }
    });

嘗試以下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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM