Results 1 to 6 of 6
  1. #1
    wilnicm is offline Member
    Join Date
    May 2014
    Posts
    22
    Rep Power
    0

    Default does anyone knows why this statement when i do an if statement evaluates to false

    Hi i'm new to java
    programing. Why does this statement

    Private class HC implements ItemListener {

    public void itemStateChanged(ItemEvent event) {

    if (jCheckbox.isSelected){
    do something

    }
    else {
    do soemthing else
    }
    }
    }

    i'm checking to see if the checkbox is selected and when it is selected to do something.
    but it seems to always to

    the section of the code that do soemthing else. which doesnt' make any sense to me?

    i'm
    Last edited by wilnicm; 05-18-2014 at 01:52 AM. Reason: want to add more information

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: does anyone knows why this statement when i do an if statement evaluates to false

    I presume you meant jCheckbox.isSelected(). Based on what you shown, it is hard to tell . Perhaps the checkbox was not really checked.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    wilnicm is offline Member
    Join Date
    May 2014
    Posts
    22
    Rep Power
    0

    Default Re: does anyone knows why this statement when i do an if statement evaluates to false

    Quote Originally Posted by jim829 View Post
    I presume you meant jCheckbox.isSelected(). Based on what you shown, it is hard to tell . Perhaps the checkbox was not really checked.

    Regards,
    Jim
    Well I first debug the program and check the check box i'm evaluating and put a line break where it evaluates the if statement. After the if statement gets evaluated it just goes to the else statements?
    instead of the true part.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: does anyone knows why this statement when i do an if statement evaluates to false

    Java Code:
    if (jCheckbox.isSelected){
    That code should not compile. If jCheckbox is an instance of the JCheckBox class, that class does not have a boolean field named isSelected. There is a method by that name but that requires ()s after the name.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    wilnicm is offline Member
    Join Date
    May 2014
    Posts
    22
    Rep Power
    0

    Default Re: does anyone knows why this statement when i do an if statement evaluates to false

    Quote Originally Posted by Norm View Post
    Java Code:
    if (jCheckbox.isSelected){
    That code should not compile. If jCheckbox is an instance of the JCheckBox class, that class does not have a boolean field named isSelected. There is a method by that name but that requires ()s after the name.

    i ended up figuring it out,
    seems i have to use event.getStateChange()==1 to make it work.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: does anyone knows why this statement when i do an if statement evaluates to false

    Well, that may work but that is not the problem. You need to find out why the original solution is not working. Are you certain you are using the proper instance of JCheckBox in your itemStateChanged method? Best to try the following:
    Java Code:
     @Override
       public void itemStateChanged(ItemEvent ie) {
          JCheckBox cb = (JCheckBox)ie.getSource();
          if (cb.isSelected()) {
             // do something
          } else {
             // do something else
          }
       }
    If the above works, then you need to find out why it didn't work before. It could be you were using the wrong variable or redeclaring it. Once you find out what caused the problem, you will be less likely to repeat it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Statement and prepared statement
    By suhaas.mohandos@gmail.com in forum JDBC
    Replies: 10
    Last Post: 01-30-2014, 01:44 PM
  2. Replies: 3
    Last Post: 11-19-2012, 08:12 PM
  3. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  4. If statement executing when false...
    By ribbs2521 in forum New To Java
    Replies: 6
    Last Post: 10-26-2009, 05:19 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 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
  •