ButtonTabComponent 在 JTabbedPane 中添加 AquaTabbedPaneCopyFromBasicUI$TabContainer

发布于 2024-12-16 22:01:44 字数 2822 浏览 2 评论 0原文

我基本上尝试在 jtabbedpane 的每个选项卡中添加一堆 JPanel,并且它本身工作得很好。但是,当遵循向每个选项卡添加关闭按钮的 Javatutorial 时,它会在第一次(且仅是第一次)添加按钮后将这个奇怪的 tabContainer 添加到 jtabbedpane 中。我意识到 Aqua 与 mac 相关,但问题仍然出现在 linux 和 windows 上。

我基本上是这样做的:

JTabbedPane pane;

this.add("channel",new JTextArea("texttexttext"));

ButtonTabComponent ctb = new ButtonTabComponent(pane);

this.setTabComponentAt(pane.indexOfTab("channel"),ctb);

第一个选项卡也可以完美工作,但由于 AquaTabbedPaneCopyFromBasicUI$TabContainer,接下来添加的所有选项卡都被推送了一个索引,如您所见在屏幕截图中。因此,当我试图将它们从选项卡窗格中取出来更新一些信息时(检查下面链接的代码),我无法投射它,因为从indexOfTab(通道)返回的索引是aqua的索引。

调试的屏幕截图和jtabbedPane 数组:http://server.westman.no/free/ Skjermbilde%202011-11-18%20kl.%2012.32.02.png

更糟糕的是我无法删除选项卡窗格中的索引 2,我只是得到 outOfBoundsException,但我可以使用 getComponent(2),(这就是我第一次发现错误的地方,因为我可以 getComponent(title) 并且它尝试返回 Aqua...并且然后我无法将其转换为想要的单选项卡对象)

这是一个超现实的问题,可能看起来像一些 youHaveToHaveDoneSomethingElseWrong,但我已经这样做了一个星期,并且在经历了有几个人在看它,我还没有接近解决方案。 (当然,这可能是一个很难解决的问题。)

希望这对某人有意义!

有问题的代码: tabHandler : http://apps.netcrawlr.net/p/pastebin.php?show =41

如果您想查看其他参考内容,请发表评论。

sscce:

    package jtabbedtest;

        import javax.swing.JFrame;
        import javax.swing.JTabbedPane;
        import javax.swing.JTextArea;

        /**
         *
         * @author hallvardwestman
         */
        public class Jtabbedtest {

            /**
             * @param args the command line arguments
             */
            public static void main(String[] args) {
                // TODO code application logic here


                JFrame jf = new JFrame();
                JTabbedPane jt = new JTabbedPane();
         //debugChecks for whats in jtabbedpane       
         Object[] o = jt.getComponents();
                jf.add(jt);

                jt.addTab("a",new JTextArea("a"));
                int tabIndex = jt.indexOfTab("a"); 

                ButtonTabComponent ctb = new ButtonTabComponent(jt);
                jt.setTabComponentAt(tabIndex, ctb);
                /*
                 * adding closebutton
                 */
          //debugChecks for whats in jtabbedpane       
         o = jt.getComponents();
                jf.setVisible(true);

                jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


            }
        }

and just put this : http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java    in a new file called ButtonTabComponent

Im basicly trying to add in a bunch of JPanels in each tab in the jtabbedpane, and it works fine on its own. But when following the Javatutorial on adding closingbuttons to each tab, it adds this weird tabContainer to the jtabbedpane after the first(and only the first) adding of a button. I realize Aqua is mac-related but problem still occurs linux and windows..

im basicly doing:

JTabbedPane pane;

this.add("channel",new JTextArea("texttexttext"));

ButtonTabComponent ctb = new ButtonTabComponent(pane);

this.setTabComponentAt(pane.indexOfTab("channel"),ctb);

the first tab also works perfectly, but all the next ones added is beeing pushed one index over because of the AquaTabbedPaneCopyFromBasicUI$TabContainer, as one can see in the screenshot. So when im trying to get them out of the tabbedpane to update some info(check the code linked below) i cant cast it, because the index beeing returned from indexOfTab(Channel), is the index of the aqua..

screenShot of debugging and the array of jtabbedPane: http://server.westman.no/free/Skjermbilde%202011-11-18%20kl.%2012.32.02.png

whats worse is that i cant remove index 2 in the tabbedpane, i just get outOfBoundsException, but i can use getComponent(2),(and thats where i first found the bug, because i can getComponent(title) and it tries to return the Aqua... and then i cant cast it to a singletab object which is what want)

This is a surreal problem which probably looks like some youHaveToHaveDoneSomethingElseWrong, but ive been at this for a week, and after having sevral people looking at it, im no closer to a solution. (It can ofcourse be a gotDamnUrADumbAss problem tough..)

Hope this made sense to someone!

Code in question :
tabHandler : http://apps.netcrawlr.net/p/pastebin.php?show=41

comment if you would like to see something else referenced.

sscce:

    package jtabbedtest;

        import javax.swing.JFrame;
        import javax.swing.JTabbedPane;
        import javax.swing.JTextArea;

        /**
         *
         * @author hallvardwestman
         */
        public class Jtabbedtest {

            /**
             * @param args the command line arguments
             */
            public static void main(String[] args) {
                // TODO code application logic here


                JFrame jf = new JFrame();
                JTabbedPane jt = new JTabbedPane();
         //debugChecks for whats in jtabbedpane       
         Object[] o = jt.getComponents();
                jf.add(jt);

                jt.addTab("a",new JTextArea("a"));
                int tabIndex = jt.indexOfTab("a"); 

                ButtonTabComponent ctb = new ButtonTabComponent(jt);
                jt.setTabComponentAt(tabIndex, ctb);
                /*
                 * adding closebutton
                 */
          //debugChecks for whats in jtabbedpane       
         o = jt.getComponents();
                jf.setVisible(true);

                jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


            }
        }

and just put this : http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java    in a new file called ButtonTabComponent

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世蒼涼 2024-12-23 22:01:44

目前我正在开发也使用 JTabbedPane 的应用程序,一切对我来说都工作正常,添加、删除选定的选项卡、其他选项卡和所有选项卡。

首先,您应该(不是必须)提供整数变量来保存已有选项卡的总和,假设它调用 tabCount

代码来检查 tabbedpane 中是否存在选项卡:

private boolean isTabExist(String title) {
    for (int i = 0; i < tabCount; i++) {
        if (jTabbedPane1.getTitleAt(i).equalsIgnoreCase(title)) {
            jTabbedPane1.setSelectedIndex(i);
            return true;
        }
    }
    return false;
}

插入新选项卡的代码:(在我的应用程序中,如果该选项卡已添加,则不会再次添加)

if (!isTabExist("My Tab")) {
        jTabbedPane1.insertTab("My Tab", null, yourCustomPanel, "My tab", tabCount);
        jTabbedPane1.setSelectedIndex(tabCount);
        tabCount = jTabbedPane1.getTabCount();            
    }

以删除选定的选项卡(在任何索引中)

jTabbedPane1.remove(jTabbedPane1.getSelectedIndex());  // remove selected tab
tabCount = jTabbedPane1.getTabCount();

尝试上面的代码,并告诉我您是否仍然遇到删除某些索引处的选项卡的问题...

Currently im developing app that use JTabbedPane too, and everything works fine for me, adding, removing selected tab, other tab, and all tab.

First of all, you should - not a must- provide integer variable to hold sum of the tab you already have, let's say it called tabCount

code to check if a tab exist in tabbedpane :

private boolean isTabExist(String title) {
    for (int i = 0; i < tabCount; i++) {
        if (jTabbedPane1.getTitleAt(i).equalsIgnoreCase(title)) {
            jTabbedPane1.setSelectedIndex(i);
            return true;
        }
    }
    return false;
}

code to insert new tab : (in my app, if that tab is already added, it won't add again)

if (!isTabExist("My Tab")) {
        jTabbedPane1.insertTab("My Tab", null, yourCustomPanel, "My tab", tabCount);
        jTabbedPane1.setSelectedIndex(tabCount);
        tabCount = jTabbedPane1.getTabCount();            
    }

to remove selected tab (in any index)

jTabbedPane1.remove(jTabbedPane1.getSelectedIndex());  // remove selected tab
tabCount = jTabbedPane1.getTabCount();

Try codes above and tell me if you still getting problem to remove tab at some index...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文