Checkboxes, Radio Buttons, Scrolling Lists, and Choice Lists To our growing list of window controls we now add checkboxes, radio
button groups, scrolling lists, and popup choice lists.
Before diving into the details, we present a simple demonstration program. An initial screenshot for the demo program is
below and contains:
The user and the program can both manipulate the settings of the
various window controls. The program made the
settings shown above at startup. When the
user selects the Get command, the program reads
the current settings and displays them in a message box (shown next).
When the user selects the Set
command, the program modifies the contents of the scrolling and popup lists and makes
several selections, as shown next.
Whenever the user selects, with a single click, an item in a
scrolling list, the program can respond to this selection. In the next illustration, a
message is displayed in the text area.
Any time the user double clicks an item in a scrolling list, the
program is informed of both a single click (see previous example) and a double click. Our demo program displays a message box indicating
the item double-clicked, and also updates the text area with the results of a single click
(the message box is shown next).
Now that we have seen a simple demonstration, we turn to some of the
details. CheckboxesCheckboxes are declared in a now familiar manner, for instance, Checkbox cbDriver = addCheckbox ("Driver", 1,1,1,1); Here are four frequently used CheckBox
methods:
Radio ButtonsWhen checkboxes are placed in a group, they behave like radio
buttons, meaning that selecting one automatically deselects the others. To use radio buttons, one must declare a CheckboxGroup,
several checkboxes, and then add each checkbox to the group. Later, when the program needs to know which
checkbox has been selected, it sends the getSelectedCheckbox()
message to the checkbox group control. Here
are relevant lines of code from the demo program: CheckboxGroup cbgMaritalStatus = new CheckboxGroup(); // Radio button Checkbox cbMarried = addCheckbox ("Married", 2,1,1,1); // group Checkbox cbSingle = addCheckbox ("Single", 3,1,1,1); // Checkbox cbDivorced = addCheckbox ("Divorced", 4,1,1,1); // // Place the married, single, and divorced checkboxes in a // radio button group, and select the "Single" option cbMarried.setCheckboxGroup (cbgMaritalStatus); cbSingle.setCheckboxGroup (cbgMaritalStatus); cbDivorced.setCheckboxGroup (cbgMaritalStatus); cbSingle.setState (true); ... cbgMaritalStatus.getSelectedCheckbox().getLabel() The following table contains a summary of the methods just discussed:
Scrolling ListsThe strings in a scrolling list can be manipulated by the user and by
the program. When the user clicks on a
string, it is automatically highlighted. Later
the program can query the list to determine which string the user selected. When the user double clicks on a string, it is
highlighted, and the program is notified immediately.
The program is also able to select, and thus highlight, a string. Finally, the program can add strings to and remove
strings from the list. Here are some
fragments of code that show how the demo program declares and manipulates a scrolling
list. List ltHobbies = addList (1,2,1,3); // Scrolling list // Load up the list control ltHobbies.add ("Swimming", 999); // 999 is larger than list size ltHobbies.add ("Reading", 999); // therefore string added to ltHobbies.add ("Golf", 999); // end of list ltHobbies.add ("Fishing", 999); ltHobbies.add ("Dusting", 999); ltHobbies.add ("Cooking", 999); ltHobbies.add ("Movies", 999); ltHobbies.select (2); // select the 3rd string // Determine which string has been selected and retrieve it i = ltHobbies.getSelectedIndex(); selection = ltHobbies.getItem(i); ltHobbies.remove (3); // Remove 4th item, "Fishing" ltHobbies.add ("Tennis", 3); // Add "Tennis" in 4th position ltHobbies.select (3); // Select 4th item The next table summarizes the commonly used Java List
methods. Note that positions within a list
are zero based.
The method addList
is provided for your convenience by BreezyGUI. This package also provides two methods for
responding to events that occur in scrolling lists, as shown in the next table.
Applications are free to ignore single clicks in a scrolling list and
omit the implementation of listItemSelected. However, if the application is to ignore double
clicks, the programmer must implement a listDoubleClicked
method that does nothing. Otherwise, a
message box displays a reminder to implement this method. Popup Choice ListsA popup choice list and a scrolling list look very different, but in
other respects they have much in common. Their
main difference, from the perspective of this demonstration, is that double clicking on a
popup list has no effect. The next table
summarizes some useful Choice
methods:
Code for the DemoHere is the complete code for the demonstration program. import java.awt.*; import BreezyGUI.*; public class Test extends GBFrame{ // Declare the window controls. The variables have been given prefixes // that indicate their type. Doing this is not necessary, but hopefully // it makes the program more readable. // // Prefix Type of Control // cb Checkbox // ch Choice // lt List // bt Button
Checkbox cbDriver = addCheckbox ("Driver", 1,1,1,1); // Checkbox
CheckboxGroup cbgMaritalStatus = new CheckboxGroup(); // Radio button Checkbox cbMarried = addCheckbox ("Married", 2,1,1,1); // group Checkbox cbSingle = addCheckbox ("Single", 3,1,1,1); // Checkbox cbDivorced = addCheckbox ("Divorced", 4,1,1,1); //
Choice chHobbies = addChoice (4,2,1,1); // Popup choice list
List ltHobbies = addList (1,2,1,3); // Scrolling list TextArea ltHobbyOut = addTextArea ("", 1,3,1,3); // Output of selected // list item.
Button btGet = addButton ("Get", 5,1,1,1); // Command buttons Button btSet = addButton ("Set", 5,2,1,1);
// Constructor public Test(){ // Load up the list control ltHobbies.add ("Swimming", 999); // 999 larger than list size ltHobbies.add ("Reading", 999); // therefore string added to ltHobbies.add ("Golf", 999); // end of list ltHobbies.add ("Fishing", 999); ltHobbies.add ("Dusting", 999); ltHobbies.add ("Cooking", 999); ltHobbies.add ("Movies", 999); ltHobbies.select (2); // select the 3rd string
// Load up the choice control chHobbies.insert ("Swimming", 999); // 999 larger than list size chHobbies.insert ("Reading", 999); // therefore inserts at end chHobbies.insert ("Golf", 999); // of list chHobbies.insert ("Fishing", 999); chHobbies.insert ("Dusting", 999); chHobbies.insert ("Cooking", 999); chHobbies.insert ("Movies", 999); chHobbies.select (2); // select the 3rd string
// Mark the driver checkbox cbDriver.setState (true);
// Place the married, single, and divorced checkboxes in a // radio button group, and select the "Single" option cbMarried.setCheckboxGroup (cbgMaritalStatus); cbSingle.setCheckboxGroup (cbgMaritalStatus); cbDivorced.setCheckboxGroup (cbgMaritalStatus); cbSingle.setState (true); } public void buttonClicked (Button buttonObj){ String str; int i; if (buttonObj == btGet){
// Read the data from the screen and display it in a message box.
// Examine the driver checkbox str = "Checkbox driver: " + cbDriver.getState() + "\n";
// Examine the radio button group str += "Marital status : " + cbgMaritalStatus.getSelectedCheckbox().getLabel() + "\n";
// Examine the scrolling list i = ltHobbies.getSelectedIndex(); str += "List item : " + ltHobbies.getItem(i) + "\n";
// Examine the popup choice list i = chHobbies.getSelectedIndex(); str += "Choice item : " + chHobbies.getItem(i) + "\n";
// Display the data in a message box messageBox (str);
}else{
// Modify some of the data on the screen. cbDriver.setState (false); // Uncheck driver cbMarried.setState (true); // Choose radio button "Married"
ltHobbies.remove (3); // Remove 4th item, "Fishing" ltHobbies.add ("Tennis", 3); // Add "Tennis" in 4th position ltHobbies.select (3); // Select 4th item chHobbies.remove (3); // Remove 4th item, "Fishing" chHobbies.insert ("Tennis", 3); // Add "Tennis" in 4th position chHobbies.select (3); // Select 4th item } }
public void listDoubleClicked (List listObj, String itemClicked){ // Handle double clicks on a list item. messageBox ("You double clicked " + itemClicked); }
public void listItemSelected (List listObj){ // Handle selection (single click) of a list item. int index = listObj.getSelectedIndex(); String item = listObj.getItem(index); ltHobbyOut.setText("You selected \n" + item); } public static void main (String[] args){ Frame frm = new Test(); frm.setSize (325, 175); frm.setVisible(true); } } |
| ||||