jTabbedPane new tab problem

I have a question regarding the TabbedPane, and forgive my ignorance as I've only just started. I am attempting to create a telnet/SSL application that allows you to open and close new tabs while the window's running, much like any tabbed web browser. I see there's a getTabCount() method, but it doesn't seem to work as I expected, or I'm using it incorrectly. Basically, each tab is supposed to have its own jTextArea to allow the user to connect to a remote host using a non-secure or secure method. The method that performs the action is below. If I put the "tabcount" int in the setTabComponentAt(), an index out of bounds error is returned in the stack trace and it appears to default to 0; if I put 0 in there as it is below, the stack trace goes away and the TabbedCommunicator class creates the tab with the close button. It just never seems to want to have more than one tab at a time. Any input is welcome and appreciated

privatevoid performNewTab(java.awt.event.ActionEvent evt){

int tabcount = jTabbedPane1.getTabCount();

tabcount++;

String title ="untitled_" + tabcount;

jTabbedPane1.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

jTabbedPane1.addTab(title, jScrollPane3);

jTabbedPane1.setTabComponentAt(0,new TabbedCommunicator(jTabbedPane1));

jTabbedPane1.getAccessibleContext().setAccessibleName("");

return;

}

[1674 byte] By [ScottNeibargera] at [2007-11-27 7:57:03]
# 1

In Java, indexes are 0 based. This means that when you add a tab to the tabbed pane the tab count will be 1, but to access that tab you use 0.

The easiest way to add a tab is to build your panel first and then just use the addTab(...) method to add the panel to the tabbed pane. That way you don't need to use the setTabComponentAt(..) method. You would generally only use the setTabComponentAt(...) method when you change the contents of a specific tab.

camickra at 2007-7-12 19:38:51 > top of Java-index,Desktop,Core GUI APIs...
# 2

Before posting, I was aware of how the tab components are accessed. For whatever reason, I left out the "tabcount--;" that was there after the title for the tab. If I adjusted the code to not hard-code to 0, I still received the same stack trace error (ArrayIndexOutOfBounds) as I did before the tabcount--; was added. It's as if the tabbed pane itself can only hold one tab, and in order to have multiple tabs, you've got to have multiple tabbed panes. The setTabComponentAT() method is being used to create the closeable tab. That portion of the code works great when the application is first started, as well as when the tab is closed, then the File->New Tab menu is accessed. But if the closeable tab is still there and File->New Tab is accessed, it persists at creating only one tab, and that tab's closeable feature doesn't work..

The stack trace is:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1

at java.util.Vector.elementAt(Vector.java:427)

at javax.swing.JTabbedPane.getTabComponentAt(JTabbedPane.java:2363)

at javax.swing.JTabbedPane.setTabComponentAt(JTabbedPane.java:2340)

at clientgui.JavaCommunicationsGUI.performNewTab(JavaCommunicationsGUI.java:398)

at clientgui.JavaCommunicationsGUI.access$100(JavaCommunicationsGUI.java:41)

at clientgui.JavaCommunicationsGUI$2.actionPerformed(JavaCommunicationsGUI.java:296)

ScottNeibargera at 2007-7-12 19:38:51 > top of Java-index,Desktop,Core GUI APIs...
# 3

You have a logic error in your code somewhere and since you didn't post a short demo of what you are trying to do we won't be able to help.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 19:38:51 > top of Java-index,Desktop,Core GUI APIs...