1 00:00:00,000 --> 00:00:00,150 2 00:00:00,150 --> 00:00:03,120 >> SPEAKER 1: Let's write a program that has a graphical user interface with a 3 00:00:03,120 --> 00:00:06,510 check box, and see if we can't figure out how to listen for user 4 00:00:06,510 --> 00:00:09,680 interactions with that check box so that we can print to the screen 5 00:00:09,680 --> 00:00:12,980 whenever we hear the user check or uncheck that check box. 6 00:00:12,980 --> 00:00:15,810 >> I've gotten myself started here in advance with some skeleton code. 7 00:00:15,810 --> 00:00:17,610 Now let's go and fill in the blank. 8 00:00:17,610 --> 00:00:20,500 First, after allocating that window, I'm going to go ahead and allocate a 9 00:00:20,500 --> 00:00:24,400 GCheckBox, calling it checkbox. 10 00:00:24,400 --> 00:00:29,090 And I'm going to assign to it the return value of newGCheckBox, and I'm 11 00:00:29,090 --> 00:00:33,050 going to pass in a string of, say, "I agree." In other words, that's the 12 00:00:33,050 --> 00:00:35,420 label that will be associated with that check box. 13 00:00:35,420 --> 00:00:39,750 >> Next, let's go ahead and call setActionCommand, passing in the 14 00:00:39,750 --> 00:00:43,990 checkbox and passing in a unique identifier, like "check." In other 15 00:00:43,990 --> 00:00:47,320 words, I want to associate a unique word, somewhat arbitrarily, but 16 00:00:47,320 --> 00:00:51,470 specifically in this case "check," so that when this checkbox is checked, I 17 00:00:51,470 --> 00:00:55,520 can potentially uniquely identify that checkbox if my interface happened to 18 00:00:55,520 --> 00:00:57,540 have multiple checkboxes. 19 00:00:57,540 --> 00:01:02,820 >> Let's next add the checkbox to the user interface with addToRegion, 20 00:01:02,820 --> 00:01:07,040 passing in window, passing in checkbox, and passing in quote unquote 21 00:01:07,040 --> 00:01:10,730 "SOUTH," "SOUTH" being unique identifier defined in the Stanford 22 00:01:10,730 --> 00:01:14,060 Portable Library that simply refers to the southern region, or the bottom 23 00:01:14,060 --> 00:01:15,770 region, of a user interface. 24 00:01:15,770 --> 00:01:19,680 >> Let's next deliberately induce an infinite loop. 25 00:01:19,680 --> 00:01:22,660 Inside of this loop, let's now listen for three actions. 26 00:01:22,660 --> 00:01:25,420 One, the user trying to close the window. 27 00:01:25,420 --> 00:01:27,090 Two, the user checking the box. 28 00:01:27,090 --> 00:01:29,690 And three, the user unchecking the box. 29 00:01:29,690 --> 00:01:34,020 >> Let's first declare a GActionEvent, calling it event. 30 00:01:34,020 --> 00:01:37,740 And assign to it the return value of waitForEvent. 31 00:01:37,740 --> 00:01:42,850 Passing in ACTION_EVENT, a constant declared in the Stanford Portable 32 00:01:42,850 --> 00:01:45,970 Library that indicates that I'd like to listen for an action. 33 00:01:45,970 --> 00:01:52,410 >> Let's next check if, getEventType, passing in event, equals equals 34 00:01:52,410 --> 00:01:56,970 WINDOW_CLOSED, another constant declared in the Stanford portable 35 00:01:56,970 --> 00:02:00,040 library that indicates that, indeed, the window has been closed. 36 00:02:00,040 --> 00:02:04,000 Then let's break out of my otherwise infinite loop. 37 00:02:04,000 --> 00:02:07,650 >> Lastly, let's listen for the user checking the box or unchecking the box 38 00:02:07,650 --> 00:02:08,630 as follows. 39 00:02:08,630 --> 00:02:15,010 If string compare of getActionCommand, passing in event. 40 00:02:15,010 --> 00:02:19,250 Comma quote unquote "check" equals equals 0. 41 00:02:19,250 --> 00:02:22,360 In other words, if the unique identifier associated with the 42 00:02:22,360 --> 00:02:25,760 checkbox that's been checked is, quote unquote, "check," the unique 43 00:02:25,760 --> 00:02:28,210 identifier that I supplied earlier, then let's 44 00:02:28,210 --> 00:02:29,460 proceed to do the following. 45 00:02:29,460 --> 00:02:31,770 46 00:02:31,770 --> 00:02:40,570 >> If isSelected checkbox, then I'm going to print out, for instance, checkbox 47 00:02:40,570 --> 00:02:44,020 was checked. 48 00:02:44,020 --> 00:02:47,270 Else I'm going to assume that the checkbox was unchecked, and so I'm 49 00:02:47,270 --> 00:02:49,230 going to print out this instead. 50 00:02:49,230 --> 00:02:53,520 Printf checkbox was unchecked. 51 00:02:53,520 --> 00:02:56,670 In other words, isSelected is another function defined in the Stanford 52 00:02:56,670 --> 00:02:59,070 Portable Library that does exactly that-- check if 53 00:02:59,070 --> 00:03:00,970 a checkbox is selected. 54 00:03:00,970 --> 00:03:03,670 >> Let's now save, compile, and run this program. 55 00:03:03,670 --> 00:03:05,285 Make checkbox. 56 00:03:05,285 --> 00:03:07,610 ./checkbox. 57 00:03:07,610 --> 00:03:08,820 There's my user interface. 58 00:03:08,820 --> 00:03:11,030 And indeed, there is my checkbox. 59 00:03:11,030 --> 00:03:15,680 If I now click it, "checkbox was checked." And if I click it again, 60 00:03:15,680 --> 00:03:17,690 "checkbox was unchecked." 61 00:03:17,690 --> 00:03:19,488