Results 1 to 10 of 10
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Which event for as they select the radio button?

    Hi!
    I have a JCheckBox on my form and I have an event as such:

    switchChkbox = new JCheckBox("New check box");
    switchChkbox.addChangeListener(new ChangeListener() {
    public void stateChanged(ChangeEvent arg0) {
    doRyanSwitch();
    }
    });
    The problem is I don't even have to click the checkbox, as I mouse over it the event fires :eek:

    Which event handler do I need so that it fires only when someone clicks the box to check/uncheck it?

    Thanks!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I belie you can test what the state of the check box is, make it work when the state is clicked.

    http://download.oracle.com/javase/tu....html#checkbox
    Last edited by sunde887; 02-18-2011 at 02:22 AM.

  3. #3
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    I belie you can test what the state of the check box is, make it work when the state is clicked.
    Whoa! Thats a lot of complex stuff for a newb like be, isnt there a short answer?
    For example what kind of code do you use when you have a checkbox on your form?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I edited in a link to a tutorial on using buttons. I have never used them yet, but a button should have some sort of state which is a boolean value, for example if it's true, the button is checked, if it is false, it is not checked.

    You need to listen for when the checkbox state is true. If it's true you want to do something. The tutorial contains some code examples and should be great to help you.

    Java Code:
    JCheckBox jc = new JCheckBox();
    if(jc.isSelected()){
      //do something
    }
    This isn't what you need exactly, but it shows what you want. Check if it's selected and do something if it is.
    Last edited by sunde887; 02-18-2011 at 02:32 AM.

  5. #5
    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 N00Bie View Post
    Hi!
    I have a JCheckBox on my form and I have an event as such:



    The problem is I don't even have to click the checkbox, as I mouse over it the event fires :eek:

    Which event handler do I need so that it fires only when someone clicks the box to check/uncheck it?

    Thanks!
    ItemListeners work well with JCheckBoxes.

  6. #6
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Ok, will check it out.

    In the meantime if anyone has any suggestions please feel free to pass them along :)

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Personally I wouldn't add a listener to a check box. They are generally for users to select an option on a form, such as gender. What exactly is supposed to happen when I click "Male"? Usually nothing happens until all options are chosen and all fields completed and user clicks a submit button.

  8. #8
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Trying to add an itemListener gives me this error:
    The method addItemListener(ItemListener) in the type AbstractButton is not applicable for the arguments
    (GameForm)

  9. #9
    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 N00Bie View Post
    Trying to add an itemListener gives me this error:

    The method addItemListener(ItemListener) in the type AbstractButton is not applicable for the arguments
    (GameForm)
    The error message is telling you what's wrong: Don't pass a GameForm object into the addItemListener method, pass an ItemListener object (kind of makes sense, doesn't it?).


    Quote Originally Posted by Junky View Post
    Personally I wouldn't add a listener to a check box. They are generally for users to select an option on a form, such as gender. What exactly is supposed to happen when I click "Male"? Usually nothing happens until all options are chosen and all fields completed and user clicks a submit button.
    Makes a lot of sense to me.

  10. #10
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Ok, it was firing even if you moused over it, so I just modified the end method to this (and it works) :
    public void doRyanSwitch(){
    if(switchChkbox.isSelected()){
    but1.setText("A");}
    else
    {but1.setText("1st/Root");}
    }
    So now the function does not care if it keeps firing, only when its checked or unchecked.

Similar Threads

  1. Radio Button
    By S-NESH in forum New To Java
    Replies: 6
    Last Post: 02-04-2011, 05:03 PM
  2. Radio Button help!
    By javanator in forum New To Java
    Replies: 3
    Last Post: 04-25-2010, 08:01 PM
  3. Select one of X radio buttons
    By Adrien in forum AWT / Swing
    Replies: 1
    Last Post: 02-25-2010, 06:30 PM
  4. How to use SWT Radio Button
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:25 PM
  5. Reacting Radio button event
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:46 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
  •