• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enable checkbox question

 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my beginning Java Programming workbook there is an example of toggling between setting the checkbox enabled or disabled, but I'm not quite sure I get it. The instructor couldn't really explain it to me except to say that it is toggling because of the ! operator:



Can anyone explain this to me? What does the !checkbox.isEnabled() method return to checkbox.setEnabled that does the opposite of what is is now?

Thanks, Vonuu
 
Frankey James
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
checkbox.isEnabled // returns a bool of true of false.

checkbox.setEnabled() //requires a bool to determine if the checkbox should be enabled or not

!checkbox.isEnabled means to inverse (or toggle) the CURRENT value of checkbox.isEnabled by negating the current value witht he ! operator

So checkbox.setEnabled(!checkbox.isEnabled()); so this is changing the current state (enabled or not) to the opposite of what it currenlt is

more lines of code example could look like this:

//disable checkbox if it's enabled, and enable it if it's currently not enabled
if(checkbox.isEnabled == true){

checkbox.setEnabled(false);

}else if(checkbox.isEnabled == flase){

checkbox.setEnabled(true);
}


Make cents?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good explanation (barring the dreadful pun in the last line), but never use == true or == false. They are prone to nasty errors if you manage to write = instead of ==.

If you use the CODE button as you ought, you get:I think Vonique's original example more elegant.
 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, yes that makes a lot of cents to me now! I like the pun, too.

Great explanation!

Von
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vonique Leary wrote:I like the pun, too.
Aaaargh!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic