Results 1 to 6 of 6
- 07-02-2012, 10:21 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 26
- Rep Power
- 0
How do I get a method to be called every time any checkbox gets checked?
Hi
Within my GUI, I have a large number of "JCheckBox"es.
I would like a certain method to be called each time any of the checkboxes is clicked.
The problem is that I can't say for example:
(In my Action Performed method)
if (e.getSource== checkBox1 || checkbox2 || checkbox 3 .. ...)
This wouldn't be the best way to do it, as I have many checkboxes.
Therefore, I'm looking for a better way to do it.
Any help would be appreciated.
Thanks
- 07-02-2012, 10:36 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: How do I get a method to be called every time any checkbox gets checked?
Many ways to implement. Here's one: add different actions listeners to each JCheckBox that implements what you need for said JCheckBox (in other words, don't rely on a single ActionListener to handle all). Another way might entail creating an interface, and using a Map to correlate the JCheckBox to the appropriate implementation of the interface.
- 07-05-2012, 11:48 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 2
- Rep Power
- 0
Re: How do I get a method to be called every time any checkbox gets checked?
assuming you mean literally ANY of the checkboxes:
if (e.getSource() instanceof JCheckBox)
-
Re: How do I get a method to be called every time any checkbox gets checked?
Any time I see instanceof used like this, I think that the code needs to be re-designed as this makes for very fragile code, code that easily breaks with the smallest of changes. To the original poster, doWhile's suggestions are much better than StaticRich's, and I recommend you stick with them.
- 07-12-2012, 08:53 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 26
- Rep Power
- 0
Re: How do I get a method to be called every time any checkbox gets checked?
Thanks everyone
I tried the instanceof suggestion but it didn't work.
I feel that the interface option is quite difficult to implement.
I think it might be simpler to do it this way:
I have put the checkboxes into an ArrayList. I need to check if e.getSource is any of the checkboxes in this ArrayList. Please can someone let me know if there's a method that does this (ie to see if the selected checkbox is in the ArrayList).
Thanks
- 07-17-2012, 07:01 PM #6
Member
- Join Date
- Feb 2012
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
Why does it seem this method isn't being called?
By Appel in forum New To JavaReplies: 7Last Post: 05-03-2012, 09:53 AM -
What method to use to get VALUE of the only checked radio in the current page
By lse123 in forum Advanced JavaReplies: 1Last Post: 12-16-2010, 02:39 PM -
Paint(Graphics g) method is not called on its run.
By vsanandan in forum Java 2DReplies: 5Last Post: 10-22-2010, 01:55 PM -
Client freezes when method is called
By chyrl in forum Advanced JavaReplies: 9Last Post: 07-26-2010, 09:14 PM -
Why the paint() method is called two times ?
By supremo in forum New To JavaReplies: 4Last Post: 06-03-2010, 06:21 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks