Results 1 to 12 of 12
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By KevinWorkman

Thread: actionListener & setSelected(true)

  1. #1
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default actionListener & setSelected(true)

    I have 3 radio buttons added to a buttonGroup.

    I created action listeners for each one of the buttons.

    When I clicks on the buttons from the screen, the listeners work fine.

    When I do setSelected(true) programatically on these buttons, why my listeners never get fire?

    Thanks!

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    instead of actionListener try to use ChangeListener

  3. #3
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default

    I can't use changeListner because I only want it to happen once when the button is selected.

    Does anybody know if actionListener should or should not fire with setSelected(true)?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    No it shouldn't as far as I know.
    You get a state change (because that's what it is), but not an action (because it isn't).

    You can always fire the event yourself, though that would be a bit of a pain if you setSelected() alot.

  5. #5
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Hi new2Java2009,

    Can't help you with this problem, but a humble suggestion.

    According to Java coding standards, your setter method should be named isSelected rather than setSelected if the argument is of boolean type.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    Quote Originally Posted by subith86 View Post
    Hi new2Java2009,

    Can't help you with this problem, but a humble suggestion.

    According to Java coding standards, your setter method should be named isSelected rather than setSelected if the argument is of boolean type.
    That is absolutely not true. What coding standards are these?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    It is not required, but recommended. Have read it in many books including SCJP by Kathy Sierra.
    But i was wrong about setter methods. "is" is recommened for getter methods. These links tell the same thing
    Java Beans naming convention for getter and setter methods (SCJP forum at JavaRanch)
    naming methods and variables : Java Glossary

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

    Default

    Quote Originally Posted by subith86 View Post
    .... but a humble suggestion.

    According to Java coding standards, your setter method should be named isSelected rather than setSelected if the argument is of boolean type.

    Quote Originally Posted by subith86 View Post
    It is not required, but recommended. Have read it in many books including SCJP by Kathy Sierra.
    But i was wrong about setter methods. "is" is recommened for getter methods. These links tell the same thing
    Java Beans naming convention for getter and setter methods (SCJP forum at JavaRanch)
    naming methods and variables : Java Glossary
    No I don't believe that they do, not for setter methods, and I fear that in your initial post above you are confusing getter with setter methods.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    No I don't believe that they do, not for setter methods, and I fear that in your initial post above you are confusing getter with setter methods.
    Even Sun (Oracle?) itself sinned against that rule, e.g. getInheritsPopupMenu() in the JComponent class (there are more examples).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    bikashg is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default Re: actionListener & setSelected(true)

    This should work :
    radiobutton.doClick();


    Quote Originally Posted by new2java2009 View Post
    I have 3 radio buttons added to a buttonGroup.

    I created action listeners for each one of the buttons.

    When I clicks on the buttons from the screen, the listeners work fine.

    When I do setSelected(true) programatically on these buttons, why my listeners never get fire?

    Thanks!

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: actionListener & setSelected(true)

    Reporting spam.
    DarrylBurke likes this.

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: actionListener & setSelected(true)

    Quote Originally Posted by Tolls View Post
    Reporting spam.
    Thanks. Spammer zapped.
    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. while(true)
    By ravian in forum New To Java
    Replies: 7
    Last Post: 06-29-2011, 07:05 AM
  2. true && !true
    By SirFalcon in forum New To Java
    Replies: 3
    Last Post: 10-29-2009, 03:33 AM
  3. which one of the following is true about interface?
    By makpandian in forum New To Java
    Replies: 3
    Last Post: 06-30-2009, 01:23 AM
  4. A true newbies problem.
    By Kaz in forum New To Java
    Replies: 3
    Last Post: 06-25-2009, 07:12 AM
  5. setVisible(true) does not work
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 2
    Last Post: 10-27-2007, 08:32 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
  •