簡體   English   中英

JTabbedPane-具有關閉按鈕參考的選項卡

[英]JTabbedPane - tab with close button reference

我想在JAVA中建立一個帶標簽的界面,每個標簽上都帶有關閉按鈕。 為此,我使用了以下類: ButtonTabComponent

我的GUI上有一個按鈕,用於創建新標簽。 假設我按了“新標簽”按鈕4次,因此創建了4個標簽:

| 標簽0 | 標簽1 | 標簽2 | 標簽3 | (每個選項卡都包含一個關閉按鈕)

現在,我決定要關閉選項卡1並出現問題,當我關閉中間選項卡時,所有索引都重新排序-這意味着:

| 標簽0 | 標簽2 | 標簽3 | (選項卡2的索引為1)

如果現在嘗試創建一個新選項卡,則會創建該選項卡,但會創建新的選項卡:

| 標簽0 | 標簽2 | 標簽3 | 標簽1 | (選項卡1沒有關閉按鈕)

如果再次單擊“新建選項卡”,則會得到:

| 標簽0 | 標簽2 | 標簽3 | 標簽1 | 標簽4 | (選項卡4很好,它有一個關閉按鈕)

現在,我決定關閉選項卡2,然后得到:

| 標簽0 | 標簽3 | 標簽1 | 標簽4 | (選項卡3現在將具有索引1)

如果我創建一個新標簽:

| 標簽0 | 標簽3 | 標簽1 | 標簽4 | 標簽1 | (最后一個標簽也沒有關閉按鈕)。

我相信這是由索引引起的,我在stackoverflow上讀了一個類似的問題: stackoverflow.com/questions/15312252/jtabbedpane-arrayindexoutofboundsexception ,可能的解決方案是:

將對選項卡項的引用(而不是其在選項卡式窗格中的索引)傳遞給偵聽器。

我不確定該怎么做。 如果有人有任何暗示,我將非常感謝。 我需要為每個選項卡保持可靠的引用,因為每個選項卡都會打開一個文件,並且具有保存到文件的能力,而且選項卡索引顯然不可靠。

編輯:我在代碼中添加新標簽的方式是:

...//main class
private final JTabbedPane pane = new JTabbedPane();
//I am using an array to store the tabs created
//I initialize the array with false. the idea was that when a new tab get created, one item in the array
//gets the true value. when the tab is closed, the array item (based on the index) is set back to false
arrTabList = new boolean[10];
        for(int i=0; i<10; i++){
            arrTabList[i] = false;
        }

...


public void newFile()  //this function opens a new tab
{
//parse the array to check the first item with false status
    for(int i=0; i<10; i++){
        if(!arrTabList[i]) {
            System.out.println("false");
            PanelCounter = i;
            break;
            }
    }
    newTab t = new newTab();   //object which contains the tab content (a bunch of graphical components, input fields mostly)
    pane.add("New Entry" + PanelCounter, t.createContentPane());   //adds the new tab to the main window
    pane.setTabComponentAt(PanelCounter, new ButtonTabComponent(pane, this));
    arrTabList[PanelCounter] = true;  //sets the item array to true
}

//when a tab is closed, this function is called in the listener:
public void decreaseCounter(int i)
{
        arrTabList[i] = false;  //sets the item array back to false
}

錯誤在於通話

pane.setTabComponentAt(PanelCounter, new ButtonTabComponent(pane, this));

您不想將按鈕添加到選項卡索引PanelCounter而是添加到剛剛創建的按鈕中。 可以使用getTabCount()獲得它的索引,這當然是太高了,因此:

pane.setTabComponentAt(pane.getTabCount()-1, new ButtonTabComponent(pane, this));

我正在檢查您的代碼,我可以完成使用

pane.remove(pane.getSelectedComponent());

在actionPerformed方法中。

在這里,我正在創建JTabbedPane選項卡,每個選項卡都有一個關閉按鈕。 單擊關閉按鈕后,只有相應的選項卡關閉。

//This is where a tab is created in some other function of same class
jtp.addTab("Create",new JPanel());  //jtp is a global JTabbedPane variable
int index = jtp.indexOfTab("Create");
jtp.setTabComponentAt(index,createTabHead("Create"));   

public JPanel createTabHead(String title)
{
    final String st=title;
    JPanel pnlTab = new JPanel();
    pnlTab.setLayout(new BoxLayout(pnlTab,BoxLayout.LINE_AXIS));
    pnlTab.setOpaque(false);
    JButton btnClose = new JButton("x");
    JLabel lblTitle = new JLabel(title+"    ");
    btnClose.setBorderPainted(false);
    btnClose.setOpaque(false);

    btnClose.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
             int i; 
            for(i=0;i<=jtp.getTabCount()-1;i++)//To find current index of tab
            {
            if(st.equals(jtp.getTitleAt(i)))
                    break;
            }                   
               jtp.removeTabAt(i);
             }
            });

     pnlTab.add(lblTitle);
    pnlTab.add(btnClose);
    return pnlTab;
}

暫無
暫無

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

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