如何在现有标签之间插入新标签而不删除任何标签?

时间:2014-04-21 10:01:52

标签: java swing jtabbedpane

我需要一个方法,可以在指定的索引应该向右移动后插入指定索引的新选项卡和制表符。

我不想删除新标签后的所有标签并将其重新插入。我只想在现有的标签之间添加新标签。

代码:

public class MainTabbedPane extends JTabbedPane {

    private SyntaxHighlighterManager syntaxHighlighterManager;
    private Map<Integer, Rectangle> tabsBounds = new HashMap<>();

    public MainTabbedPane() {
        this.syntaxHighlighterManager = SyntaxHighlighterManager.getInstance();

        Map<String, Action> actions = MainFrame.getInstance().getActions();
        Action closeTabAction = actions.get(CloseTabAction.CLOSE_TAB);
        Action closeAllTabsAction = actions.get(CloseAllTabsAction.CLOSE_ALL_TABS);
        Action closeAllTabsButThis = actions.get(CloseAllTabsButThisAction.CLOSE_ALL_BUT_THIS);

        super.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                (KeyStroke) closeTabAction.getValue(Action.ACCELERATOR_KEY), CloseTabAction.CLOSE_TAB);
        super.getActionMap().put(CloseTabAction.CLOSE_TAB, closeTabAction);
        super.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                (KeyStroke) closeAllTabsAction.getValue(Action.ACCELERATOR_KEY), CloseAllTabsAction.CLOSE_ALL_TABS);
        super.getActionMap().put(CloseAllTabsAction.CLOSE_ALL_TABS, closeAllTabsAction);
        super.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                (KeyStroke) closeAllTabsButThis.getValue(Action.ACCELERATOR_KEY),
                CloseAllTabsButThisAction.CLOSE_ALL_BUT_THIS);
        super.getActionMap().put(CloseAllTabsButThisAction.CLOSE_ALL_BUT_THIS, closeAllTabsButThis);

        // super.setUI(new MainTabUI());
        // TabReorderHandler.enableReordering(this);
    }

    /**
     * Central method for adding new tab.
     * 
     * @param title
     * @param fileViewer
     * @param tip
     */
    private void addNewTab(String title, Container fileViewer, String tip) {
        if (fileViewer != null) {
            super.addTab(title, null, fileViewer, tip);
            // icon is set in tabComponent MainTabComponent
            super.setTabComponentAt(super.getTabCount() - 1, new MainTabComponent(title, this));
            tabsBounds.put(super.getTabCount() - 1, super.getUI().getTabBounds(this, super.getTabCount() - 1));
        }
    }

方法addNewTab添加新标签。

谢谢!

1 个答案:

答案 0 :(得分:2)

JTabbedPane中的方法:

public void insertTab(String title,
         Icon icon,
         Component component,
         String tip,
         int index)

在给定索引处插入给定组件的新选项卡,由给定标题和/或图标表示,其中任何一个都可以为空。

参数:

title - the title to be displayed on the tab
icon - the icon to be displayed on the tab
component - the component to be displayed when this tab is clicked.
tip - the tooltip to be displayed for this tab
index - the position to insert this new tab (> 0 and <= getTabCount())

抛出:

IndexOutOfBoundsException - if the index is out of range (< 0 or > getTabCount())

来源: http://docs.oracle.com/javase/7/docs/api/javax/swing/JTabbedPane.html#insertTab%28java.lang.String,%20javax.swing.Icon,%20java.awt.Component,%20java.lang.String,%20int%29