JTabbedPane - 具有关闭按钮引用的选项卡 [英] JTabbedPane - tab with close button reference

查看:191
本文介绍了JTabbedPane - 具有关闭按钮引用的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JAVA中构建一个标签式界面,每个标签上都有关闭按钮。为此,我使用了以下类: ButtonTabComponent

I want to build a tabbed interface in JAVA, with close button on each tab. For that I have used the following class: ButtonTabComponent

我的GUI上有一个按钮创建一个新标签。假设我按下新标签按钮4次,因此创建了4个标签:

I have a button on my GUI which creates a new tab. Let's say I press the New Tab button 4 times, therefore 4 tabs are created:

|标签0 |选项卡1 |标签2 |选项卡3 | (每个选项卡包含一个关闭按钮)

| tab 0 | tab 1 | tab 2 | tab 3 | (each tab contain a close button)

现在,我决定关闭选项卡1,当我关闭时,问题出现了中间选项卡,所有索引都重新排序 - 意味着:

Now, I decide I want to close tab 1 and the problem appears, when I am closing a middle tab, all indexes are reordered - meaning that:

|标签0 |标签2 |选项卡3 | (选项卡2将具有索引1)

| tab 0 | tab 2 | tab 3 | (tab 2 will have index 1)

如果我现在尝试创建新选项卡,则会创建选项卡但是新选项卡:

If I try now to create a new tab, the tab is created but new tab:

|标签0 |标签2 |标签3 |选项卡1 | (选项卡1没有关闭按钮)

| tab 0 | tab 2 | tab 3 | tab 1| (tab 1 has no close button)

如果我再次点击新标签,我会得到:

If I click again New Tab, I get:

|标签0 |标签2 |标签3 |选项卡1 |选项卡4 | (标签4很好,它有一个关闭按钮)

| tab 0 | tab 2 | tab 3 | tab 1 | tab 4 | (tab 4 is fine, it has a close button)

现在我决定关闭标签2,我得到:

Now I decide to close tab 2 and I get:

|标签0 |标签3 |选项卡1 |选项卡4 | (选项卡3现在将具有索引1)

| tab 0 | tab 3 | tab 1| tab 4| (tab 3 will now have index 1)

如果我创建新选项卡:

<强> |标签0 |标签3 |选项卡1 |标签4 |选项卡1 | (最后一个标签没有关闭按钮)。

| tab 0 | tab 3 | tab 1| tab 4| tab 1 | (the last tab again has no close button).

我认为这是由索引引起的,我在类似的问题中读到了在stackoverflow上: stackoverflow.com/questions/15312252/jtabbedpane-arrayindexoutofboundsexception 可能的解决方案是:

I believe that this is caused by the index and I read in a similar question, here on stackoverflow : stackoverflow.com/questions/15312252/jtabbedpane-arrayindexoutofboundsexception that a possible solution would be to:


将对选项卡项的引用(而不是选项卡式窗格上的索引)传递给侦听器。

Pass a reference to the tab item, not its index on the tabbed pane, to the listener.

我不知道该怎么做。如果有人有任何暗示,我真的非常感激。
我需要为每个标签保留一个可靠的参考,因为每个标签都会打开一个文件,它可以保存到文件中,显然标签索引不可靠。

I am not sure how to do that. If anyone has any hint, I would really, really appreciate. I need to keep a solid reference for each tab, as each tab will open a file and it will have the ability to save to a file and obviously the tab index is not reliable.

编辑:我在代码中添加新标签的方式是:

The way I add a new tab in my code is:

...//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()获得,当然这一点太高了,因此:

You don't want to add the button to the tab index PanelCounter but to the one just created. Its index can be obtained using getTabCount(), which of course at this point is one too high, hence:

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

这篇关于JTabbedPane - 具有关闭按钮引用的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆