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?
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.
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.
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().
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.
Joined: Jan 13, 2008
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.