Question

I need a method that can insert at specified index new tab and tabs after that index should go to the right.

I don't want to remove all tabs after new one and insert them back. I only want to add new tab between existing.

The code:

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));
        }
    }

Method addNewTab adds new tab.

Thank you!

Was it helpful?

Solution

Method in JTabbedPane:

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

Inserts a new tab for the given component, at the given index, represented by the given title and/or icon, either of which may be null.

Parameters:

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())

Throws:

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

Source: 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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top