| Author |
enable checkbox question
|
Vonique Leary
Ranch Hand
Joined: Mar 24, 2008
Posts: 107
|
|
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
Joined: Sep 27, 2008
Posts: 50
|
|
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?
|
ho, ho, ho
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
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
Joined: Mar 24, 2008
Posts: 107
|
|
Thanks, yes that makes a lot of cents to me now! I like the pun, too.
Great explanation!
Von
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
Vonique Leary wrote:I like the pun, too.
Aaaargh!
|
 |
 |
|
|
subject: enable checkbox question
|
|
|