Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By doWhile

Thread: How do I get a method to be called every time any checkbox gets checked?

  1. #1
    Ami
    Ami is offline Member
    Join Date
    Feb 2012
    Posts
    26
    Rep Power
    0

    Default 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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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.
    Fubarable likes this.

  3. #3
    StaticRich is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default 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)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How do I get a method to be called every time any checkbox gets checked?

    Quote Originally Posted by StaticRich View Post
    assuming you mean literally ANY of the checkboxes:

    if (e.getSource() instanceof JCheckBox)
    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.

  5. #5
    Ami
    Ami is offline Member
    Join Date
    Feb 2012
    Posts
    26
    Rep Power
    0

    Default 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

  6. #6
    Ami
    Ami is offline Member
    Join Date
    Feb 2012
    Posts
    26
    Rep Power
    0

    Default Re: How do I get a method to be called every time any checkbox gets checked?

    Thanks for your help everyone who replied.


    I think it's sorted now.

Similar Threads

  1. Why does it seem this method isn't being called?
    By Appel in forum New To Java
    Replies: 7
    Last Post: 05-03-2012, 10:53 AM
  2. Replies: 1
    Last Post: 12-16-2010, 03:39 PM
  3. Replies: 5
    Last Post: 10-22-2010, 02:55 PM
  4. Client freezes when method is called
    By chyrl in forum Advanced Java
    Replies: 9
    Last Post: 07-26-2010, 10:14 PM
  5. Why the paint() method is called two times ?
    By supremo in forum New To Java
    Replies: 4
    Last Post: 06-03-2010, 07:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •