It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes logic problem or thread problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "logic problem or thread problem?" Watch "logic problem or thread problem?" New topic

logic problem or thread problem?

Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Hi, I have a list of dynamically created components like Jlabel and JCheckBox in a panel as below:

Label1cbk1 cbk2 cbk3

Label2cbk1 cbk2 cbk3 cbk4 cbk5 cbk6
Labelncbk1 cbk2 cbk3

I am trying to add/store these sets of values into a hashmap. I store the labelName(String) and capture
corresponding checkbox valuess(integer array) into an integer array. Then I add labelName as key and
check box values as value into hashmap. My logic works as below:

My logic is, I am checking every component. If it is a label, I am assiging it to "key"
value. As long as the next coponent of check box is not label, I am collecting it into
int array. (Basically I am scanning through every component in the panel) So if teh next element is
label, the time to add key and values into hashmap and will start scanning next line.

Inside my action Listener:

While executing, I get an exception as below:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: No such child: 3
Is it due to any thread problem or logic problem?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10817

It is a logic problem.

You are trying to increment the component index, and no component exists. That is causing the problem.
We had discussed this in your previous thread. Since the pattern is label, checkbox, label checkbox... you dont really need to increment the counter because you are always going to encounter the components in the above mentioned order.

Another way (though unnecessarily complicated) way of doing the same this is create a custom panel, which has label and checkbox embedded inside it. You add these panels to your base container. Thus, when you iterate through the base container, all the components you will get are your custom panel. Then you can invoke the appropriate methods on the custom panel to get the required values.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
yes, I got it. That makes sense. B'cos My panel would contain, one label followed by any number of check boxes. At the end of check box list, I need to add these key and values into hashmap. And then start scanning next line(ie label). This is what I am trying to do.
I will try to add the components or group them into panels and try to add it into the final dPanel. Thanks.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

Originally posted by Gopu Akraju:

Please don't do this. Ever.

First of all, String comparison should use equals: name.equals("javax.swing.JCheckBox"). Otherwise you are checking for the same reference, and that check fails in most cases.

Second, you don't need to check for the name - check for the Class object itself:

Now you notice that here I do use reference checking. This is because (unless you're using different ClassLoaders and forcing a ClassLoader to load its own version of a class - something not possible for the classes from the Java API) there is just one Class object for each Class, and it can be references through <Class Name>.class as well as through <instance of class>.getClass().

Of course you can also just use instanceof:

[ March 28, 2008: Message edited by: Rob Prime ]

How To Ask Questions How To Answer Questions
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Hi, still I have'nt found a solution for this problem. Is there anyway to collect the label name in the key field and selected checkboxes as arrayList/list in the value field of a hashmap. Thanks.
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
I tried adding every set of label and checkboxes in one panel and then adding into final panel. But when I try to add components, it is not able to count all internal panels inside the main panel Hence I went back to my original logic as below:

It is serving the purpose and printing label followed by selected check box. Now I need to add label into key value and all the selected checkbox into value followed by next set of label and check boxes. Kindly suggest. Thanks.
I agree. Here's the link:
subject: logic problem or thread problem?
It's not a secret anymore!