3

I've reccently run into issues with indexing my tabs and though I'd give it some concrete ordering by using the setComponentAt method. Here's my code:

public ContainerPane() {
    this.setLayout(new BorderLayout());
    myPlayerManagerPane = new PlayerManagerPane();
    myGameManagerPane = new GameManagerPane();
    myCharacterManagerPane = new CharacterManagerPane();

    myPaneTab = new JTabbedPane(JTabbedPane.TOP);
    myPaneTab.addTab("Character",myCharacterManagerPane);
    myPaneTab.addTab("Player",myPlayerManagerPane);
    myPaneTab.addTab("Games",myGameManagerPane);
    System.out.println(myPaneTab.getTabCount());
    //myPaneTab.setEnabledAt(1, false);
    //myPaneTab.setEnabledAt(2, false);
    myPaneTab.setComponentAt(0, myPlayerManagerPane);
    myPaneTab.setMnemonicAt(0, KeyEvent.VK_1);
    myPaneTab.setComponentAt(1, myCharacterManagerPane);
    myPaneTab.setMnemonicAt(1, KeyEvent.VK_2);
    myPaneTab.setComponentAt(2, myGameManagerPane);<---outOfBoundsException
    myPaneTab.setMnemonicAt(2, KeyEvent.VK_3);
    add(myPaneTab);
}

So for the count I have, 3 tabs (according to me, and getTabCount()), and I am counting from 0 (correct?). I'm setting the last index the last component that I have. But I still have this printing to screen:

3 <---from tabCount
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2

Where am I tripping up, is there an easier way to order my panes?

Edit: Commenting out the setComponent methods, and putting in a for loop I get this output:

There are 3 tabs!
Tab at 0 is Character
Tab at 1 is Player
Tab at 2 is Games

And uncommmenting one pair of methods at a time, I get only 2, of the one I've not overwritten, and one of the ones I've now set.

Is setComponentAt removing duplicates? Should I ever have fewer than 3 tabs with my set up? Does JTabbedPanel have odd behaviour for duplicate panes?

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173

4 Answers4

3

You get an error due to changes you are trying to do - by putting some of the panes into another position you automatically remove another tab. That is why you get the error - there is less than 3 tabs after some of the changes (you can check that outputting tabs amount after each of "setComponentAt" operations).

Simple remove all tabs you want to reorder and readd them using either addTab or insertTab - that will get the job done without any errors.

Mikle Garin
  • 10,083
  • 37
  • 59
2

Looking at source code of the setComponentAt you can see that component you're setting is removed:

                int count = getComponentCount();
                Component children[] = getComponents();
                for (int i = 0; i < count; i++) {
                    if (children[i] == page.component) {
                        super.remove(i);
                    }
                }

next this component is set as page's component (overriding last present component on that page):

 page.component = component;

So setting order of existing tab component you end up with 2 tabs.

Xeon
  • 5,949
  • 5
  • 31
  • 52
1

I'm not sure how or why setComponentAt wasn't working, though it could be the fact that tabs use a Vector and I've heard they are bad.

If you want to have a fixed order of adding, just use insertTab Like so:

myPaneTab.insertTab("Games",null, myGameManagerPane,"CharacterManagerPane",2);

Where null is for the icon, and doesn't accept it as an empty argument.

This way you know in your source code the index of the tab. The only issue with this is that you can't add a new tab at an index past the current tabCount (which you can find via getTabCount), i.e. you can't count 1,3,4 with your tabs!

I'm sure you could write a new JTabb Class, and overload the insertTab method to do something more helpful but it might obsfuscate where the tab is (I.e this way you could tell it put the tab at index 2 and it goes into index 1.

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
0

If you want your tabs in another order, add them in another order:

myPaneTab = new JTabbedPane(JTabbedPane.TOP);
myPaneTab.addTab("Player",myPlayerManagerPane);
myPaneTab.addTab("Character",myCharacterManagerPane);
myPaneTab.addTab("Games",myGameManagerPane);

Et voilà.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • That's great, but I've had issues of not being certain what the tab index is of my tabs... which is why I want to *give* the index. – AncientSwordRage Jul 31 '12 at 09:43
  • Fix the order only when you add the tabs, as above, and in the rest of the code, ask the tabbed pane for the index: `int indexOfGame = tabbedPane.indexOfTabComponent(myGameManagerPane);`. This will allow rearranging the tabs only at one place. The rest of the code will be left untouched. – JB Nizet Jul 31 '12 at 09:53