Prev Up Next
Go backward to Controlling the Text Color
Go up to Top
Go forward to Finishing Up

Responding to the Choice Menu

This is it! The last step. All you have to do is fix one remaining problem (which you may or not have even noticed while you were testing your applet a moment ago).

The one remaining problem is that when the choice is changed, the scrollbars should immediately change to reflect the new color they can be used to modify. Using the sample applet provided above, watch what happens if you just switch the choice selection back and forth between "Text" and "Background". For example, in the initial state of the applet (where the background is white and the text is black), the control boxes in the scrollbars will move from one end to the other as soon as you switch from text to background. When "Background" is selected they move to the far right to indicate all three color components have a value of 255. When "Text" is selected they move to the left to indicate the color component values are all zero.

To incorporate this behavior into your applet, you will need to add an "if" structure to the body of "choiceMade". When the new item selected is "Background" this "if" structure should choose to execute instructions to set the three scrollbar values equal to the values in the three variables that describe the background color. When the item selected is "Text", instructions to set the scrollbars according to the values of the variables that describe the text color should be executed. In either case, the instructions chosen should use the "setValue" method associated with Sliders to accomplish the change.

Within the "choiceMade" method you can determine the current setting of the menu in two ways. You can use "getSelectedItem" just as you did in the "scroll" method. To make your life a little easier, however, the browser passes the Choice's new setting as a parameter to "choiceMade". We have included a declaration for this parameter in the method header we included. The parameter name is "which".

Test this change and your applet is complete.


Prev Up Next