>> SPEAKER 1: Let's write a program that has a graphical user interface with a
check box, and see if we can't figure out how to listen for user
interactions with that check box so that we can print to the screen
whenever we hear the user check or uncheck that check box.
>> I've gotten myself started here in advance with some skeleton code.
Now let's go and fill in the blank.
First, after allocating that window, I'm going to go ahead and allocate a
GCheckBox, calling it checkbox.
And I'm going to assign to it the return value of newGCheckBox, and I'm
going to pass in a string of, say, "I agree." In other words, that's the
label that will be associated with that check box.
>> Next, let's go ahead and call setActionCommand, passing in the
checkbox and passing in a unique identifier, like "check." In other
words, I want to associate a unique word, somewhat arbitrarily, but
specifically in this case "check," so that when this checkbox is checked, I
can potentially uniquely identify that checkbox if my interface happened to
have multiple checkboxes.
>> Let's next add the checkbox to the user interface with addToRegion,
passing in window, passing in checkbox, and passing in quote unquote
"SOUTH," "SOUTH" being unique identifier defined in the Stanford
Portable Library that simply refers to the southern region, or the bottom
region, of a user interface.
>> Let's next deliberately induce an infinite loop.
Inside of this loop, let's now listen for three actions.
One, the user trying to close the window.
Two, the user checking the box.
And three, the user unchecking the box.
>> Let's first declare a GActionEvent, calling it event.
And assign to it the return value of waitForEvent.
Passing in ACTION_EVENT, a constant declared in the Stanford Portable
Library that indicates that I'd like to listen for an action.
>> Let's next check if, getEventType, passing in event, equals equals
WINDOW_CLOSED, another constant declared in the Stanford portable
library that indicates that, indeed, the window has been closed.
Then let's break out of my otherwise infinite loop.
>> Lastly, let's listen for the user checking the box or unchecking the box
as follows.
If string compare of getActionCommand, passing in event.
Comma quote unquote "check" equals equals 0.
In other words, if the unique identifier associated with the
checkbox that's been checked is, quote unquote, "check," the unique
identifier that I supplied earlier, then let's
proceed to do the following.
>> If isSelected checkbox, then I'm going to print out, for instance, checkbox
was checked.
Else I'm going to assume that the checkbox was unchecked, and so I'm
going to print out this instead.
Printf checkbox was unchecked.
In other words, isSelected is another function defined in the Stanford
Portable Library that does exactly that-- check if
a checkbox is selected.
>> Let's now save, compile, and run this program.
Make checkbox.
./checkbox.
There's my user interface.
And indeed, there is my checkbox.
If I now click it, "checkbox was checked." And if I click it again,
"checkbox was unchecked."