Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: if and else

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default if and else

    although there is no errors in my code, its not working

    PHP Code:
    if (jRadioButton1.isSelected())
          { 
              
    jLabel2.setText("right");
              
    jLabel2.setText(null);
          
    buttonGroup1.clearSelection();
             
    jLabel1.setText("how old am I");

    jRadioButton1.setText("15");
    jRadioButton2.setText("16");
    jRadioButton3.setText("17");
    jRadioButton4.setText("18");
         
          }
          else 
    jLabel2.setText("wrong"); 
    if the answer is right theradio buttons should be cleared, but its not working

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: if and else

    its not working
    What is not working?
    Add some println() statements to both the if and the else parts and see which is being executed.

  3. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 3rd, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Lightbulb Re: if and else

    Quote Originally Posted by AbdulAziz Bader View Post
    although there is no errors in my code, its not working

    PHP Code:
    if (jRadioButton1.isSelected())
          { 
              
    jLabel2.setText("right");
              
    jLabel2.setText(null);
          
    buttonGroup1.clearSelection();
             
    jLabel1.setText("how old am I");

    jRadioButton1.setText("15");
    jRadioButton2.setText("16");
    jRadioButton3.setText("17");
    jRadioButton4.setText("18");
         
          }
          else 
    jLabel2.setText("wrong"); 
    if the answer is right theradio buttons should be cleared, but its not working
    I'm not very good at RadioButtons as I haven't really used them, but would this help:

    if(JRadioButton1.isSelected() == true)

    Also, why are you setting the text of JLabel to both "right" and null?

    That will just set it to null.

    Try this:
    if (jRadioButton1.isSelected())  == true)
          { 
            //   jLabel2.setText("right"); // 
              jLabel2.setText(null);
          buttonGroup1.clearSelection();
     
    jLabel1.setText("how old am I");
     
    jRadioButton1.setText("15");
    jRadioButton2.setText("16");
    jRadioButton3.setText("17");
    jRadioButton4.setText("18");
     
          }
          else jLabel2.setText("wrong");
     
    or this, depending on which one you're looking for.
     
    if (jRadioButton1.isSelected())  == true)
          { 
              jLabel2.setText("right"); 
            //  jLabel2.setText(null); // 
          buttonGroup1.clearSelection();
     
    jLabel1.setText("how old am I");
     
    jRadioButton1.setText("15");
    jRadioButton2.setText("16");
    jRadioButton3.setText("17");
    jRadioButton4.setText("18");
     
          }
          else jLabel2.setText("wrong");
    Last edited by helloworld922; June 4th, 2010 at 07:07 PM. Reason: please use [code] tags

  5. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: if and else

    Quote Originally Posted by javapenguin View Post
    I'm not very good at RadioButtons as I haven't really used them, but would this help:

    if(JRadioButton1.isSelected() == true)
    That's just ... silly. The if statement requires a boolean condition, which isSelected() already returns.

    It is NEVER required to compare a boolean with true or false.

    db

  6. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: if and else

    Quote Originally Posted by AbdulAziz Bader View Post
    although there is no errors in my code, its not working

    if the answer is right the radio buttons should be cleared, but its not working
    Chances are you have more than one variable with the same name: the one an instance field, the other a method local variable that hides it.

    To get better help sooner, post a SSCCE. <== link

    db

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

  8. #7
    Junior Member shadihrr's Avatar
    Join Date
    Jan 2010
    Location
    home
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: if and else

    you have to try remove method to clear it and then call revalidate(); method to refresh your page (it depends on your whole code of course )

  9. #8
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: if and else

    Quote Originally Posted by shadihrr View Post
    you have to try remove method to clear it and then call revalidate(); method to refresh your page (it depends on your whole code of course )
    The question was about clearing the selection in a ButtonGroup, not about removing components from a visible GUI.

    db

  10. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    shadihrr (June 5th, 2010)

  11. #9
    Junior Member shadihrr's Avatar
    Join Date
    Jan 2010
    Location
    home
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: if and else

    The question was about clearing the selection in a ButtonGroup, not about removing components from a visible GUI.
    I meant that you have to remove a specific component by using component's remove method
    I mention that it depends on the whole code to do it!!

  12. #10
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: if and else

    Quote Originally Posted by shadihrr View Post
    I meant that you have to remove a specific component by using component's remove method
    I mention that it depends on the whole code to do it!!
    So you answered a question other than the one asked. That must have been helpful!

    db