3

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
Hwestman
  • 33
  • 5
  • 2
    Please provide an [sscce](http://sscce.org/) that exhibits the problem you describe. – trashgod Nov 18 '11 at 12:23
  • Added a quick sscce that brings the same result, if one sets a breakpoint at o = jt.getComponents(); you can see that it contains both a jtextarea and a *something else* i get AquaTabbedPaneCopyFromBasicUI$TabContainer atleast.. – Hwestman Nov 18 '11 at 15:36
  • In method SeljeIRC.tabHandler.createNewTab(String,int) shouldn't we be doing this.addTab(Channel, st); instead of this.add(Channel,st); ? – Zecas May 24 '12 at 09:29

1 Answers1

1

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...

bangandi
  • 189
  • 1
  • 3
  • 13
  • by the way, if you want to add close-ability to your tabbed pane, I suggest - like what I did - to create popup menu when you right click each tab, which showing close tab, or in addition, close other and all. I'm doing it too, and it's just works fine for me.. – bangandi May 31 '12 at 10:46
  • Sorry to say this problem was resolved with a hack, and project completed. Thanks for the response tough, it looks like your code would have done the trick. – Hwestman Jul 30 '12 at 07:40