Reputation: 3432
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!
Upvotes: 0
Views: 964
Reputation: 580
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())
Upvotes: 2