File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes ItemListener identifying which JCheckBox from array is chosen? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ItemListener identifying which JCheckBox from array is chosen?" Watch "ItemListener identifying which JCheckBox from array is chosen?" New topic
Author

ItemListener identifying which JCheckBox from array is chosen?

wayne morton
Greenhorn

Joined: May 17, 2012
Posts: 28
I create a GUI with 10 check boxes but these check boxes are from an array and all have the same name.e.g.



I tick box 4 and i want it to tell me which box i have ticked...how do i get that reference so can get the label to read out "You have ticked box 4"?

I can get it's reference in the sense that it says test, but they all say test, so which test box did you tick?


The obvious solution is to make the for loop give them all a different name.e.g.test1, test2 etc but they all have to have the same name because in the real instance of what i am doing i have a tabbed pane and on clicking "New Tab" it creates a replica of the existing tab so checkbox[1] in in tab 1, checkbox[2] is in tab 2 etc and when i tick a box i need to makes it's actions relevant only to that tab.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Using one Listener: nice non‑object‑oriented programming. Create a Listener for each button; you would probably have to write a NumberListener or similar. Add it to the button (one instance each) in the loop. You can use the same String to number the buttons, label them, and to use in the NumberListenerₑs methods.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ItemListener identifying which JCheckBox from array is chosen?