• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

logic problem or thread problem?

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Edit:
Of course you can also just use instanceof:

[ March 28, 2008: Message edited by: Rob Prime ]
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic