• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

enable checkbox question

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vonique Leary wrote:I like the pun, too.

Aaaargh!
 
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic