Results 1 to 8 of 8
  1. #1
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default JCheckBox button - what wrong with me..

    Hi everyone

    I'm new in Java and swing, and the GUI concept ,, but I'm learning

    I tried implemnt some radio - and event if I select or diselct it ( write TCSH on or off.. ) . but the problem - when ever the mouse is just getting - I get dozen of meseges "TCSH on" or off . what wrong with me ?



    Java Code:
    checkTCSH.addChangeListener(new ChangeListener() {
    				public void stateChanged(ChangeEvent e) {
    					if (checkErr.isSelected()) {
    						System.out.println("TCSH ON");
    						state_TCSH=1;
    					}
    					else{
    						System.out.println("TCSH OFF");
    					state_TCSH=1;
    				} /* end of if */} /* end of statechange*/ } /*end of changelistener*/ );

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

    Default

    what wrong with me ?
    Only your doctor and/or girlfriend can answer that, but as for your code...
    It's unusual to use a ChangeListener with a button as it detects changes of all the states of the button including (as you're finding out) rollover. It would be far better to use an ActionListener here, something that responds to the button being clicked.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    If you are new to Java Swing, it's better look at some examples first of all. Tutorial on Suns' website is really great.

  4. #4
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Only your doctor and/or girlfriend can answer that, but as for your code...
    It's unusual to use a ChangeListener with a button as it detects changes of all the states of the button including (as you're finding out) rollover. It would be far better to use an ActionListener here, something that responds to the button being clicked.
    does it work on checkbox - I want it will make action when i selcet and differnet action when de-select it

  5. #5
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default It is not solved yet... :confused:

    well , I tried to change the ChangeListener to ActionListener as detail in the code below
    but .. it just do the select option and not the de-select option

    checkTCSH is a CheckBox button

    Java Code:
    checkTCSH.addActionListener(new ActionListener() {
    				
    				public void actionPerformed(ActionEvent e) {
    					// TODO Auto-generated method stub
    					if (checkErr.isSelected()) {
    						System.out.println("TCSH ON");
    						state_TCSH=1;
    					}
    					else{
    						System.out.println("TCSH OFF");
    					state_TCSH=0;
    				} /* end of if */
    				} } /*end of changelistener*/ );

  6. #6
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Its bit confusing, you are adding listener to checkTCSH , but you are checking the state of checkErr, what is your exact requirement?

  7. #7
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    you are adding listener to checkTCSH , but you are checking the state of checkErr, what is your exact requirement?
    My error - thanks , it works now

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I think you want to use an item listener. It provides information about select/deselect events. Look at the tutorials, which are frequently referenced from the API. Components have all kinds of listeners, and the right one isn't always obvious...

Similar Threads

  1. Regarding JTree with JCheckBox and JRadioButton
    By partha_adt in forum AWT / Swing
    Replies: 0
    Last Post: 11-11-2008, 10:03 AM
  2. Issue with Jcheckbox on JTableheader
    By ram_76uk in forum AWT / Swing
    Replies: 2
    Last Post: 07-30-2008, 08:24 AM
  3. Can we add JCheckBox array into JTextArea?
    By nancyhung in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 12:07 AM
  4. jcheckbox issues need help. thanks.
    By carlos123 in forum New To Java
    Replies: 3
    Last Post: 11-05-2007, 10:37 PM
  5. Replies: 1
    Last Post: 08-08-2007, 01:25 AM

Posting Permissions

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