Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving JTabbedPane Orders

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?

like image 902
AncientSwordRage Avatar asked Mar 26 '26 23:03

AncientSwordRage


1 Answers

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.

like image 107
Mikle Garin Avatar answered Mar 29 '26 17:03

Mikle Garin