2014-04-21 71 views
0

我需要一個方法,可以在指定的索引處插入新的選項卡和選項卡後,該索引應該向右移動。如何在現有標籤之間插入新標籤而不刪除任何標籤?

我不想刪除新標籤後的所有標籤並將其插回。我只想添加現有的新選項卡。

代碼:

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增加了新的選項卡。

謝謝!

+0

顯示你的代碼,你創建tabedpane – raj

+0

tabbedPane.setTabComponentAt(0,新的JLabel( 「新標籤」 )); – raj

+0

我需要添加組件和選項卡組件。 –

回答

2

法在JTabbedPane中:

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

插入給定組件,給定索引處,由給定的標題和/或圖標,其任一者可爲null表示的新的選項卡。

參數:

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

+0

謝謝,但此方法刪除'index'選項卡。然後只能插入新標籤。我不想失去以前的標籤'索引' –

+0

@Volodia你不能有兩個標籤在同一個索引... – kleopatra

+0

@Volodia,你確定?因爲我剛剛運行了一個示例代碼,它在給定索引處創建並添加了一個新選項卡,並移動了選項卡(而不是替換它們中的任何一個)。我的tabbedPane有3個選項卡,我執行這行'jTabbedPane1.insertTab(「tab new」,null,new JPanel(),null,0);'用各種索引值測試。它並沒有取代任何標籤,而是每次都添加新標籤。 –