Text from my translation Article http://somefuture.iteye.com/blog/1197539
Original address http://download.oracle.com/javafx/2.0/ui_controls/radio-button.htm#BABBJBDA
The radiobutton class isA professional implementation of the togglebutton class. A single-choice button control can be selected or unselected. A typical radio button is placed in a group, and only one button can be selected at a time. This behavior differentiates them from the switch button, because all the switch buttons in a group can be deselected at the same time.
Figure 4-1 contains three images.In the radiobutton example,The three radio buttons are in the same group.
Figure 4-1 radiobutton sample
Description of "Figure 4-1 radiobutton sample"
You can learn more about how to implement radio buttons in applications.
Create a radio button
The radiobutton class is locatedJavafx SDKjavafx.scene.controlPackage provides two construction methods for creating single-choice buttons. Example
4-1 is to create two radio buttons. The non-parameter constructor is used to create the rb1.Settext method settings. The title of Rb2 is directly defined in the corresponding constructor.
Example 4-1 creating radio buttons
Java code
- // A radio button with an empty string for its label
- Radiobutton P4 = new radiobutton ();
- // Setting a text label
- Rb1.settext ("home ");
- // A radio button with the specified label
- Radiobutton Rb2 = new radiobutton ("calendar ");
You canSetselected MethodTo explicitly make a single-choice button selected. If you want to check whether a specific radio button is selected by the user, useIsselected method.
BecauseRadioButtonClass inheritedLabeledClass, so you can not only specify text titles for them, but also images. UseSetgraphic method to specify an image.Example
4-2 demonstrates how to implement a single button with an image in an application.
Example 4-2 creating a graphical radio button
Java code
- Image image = new image (getclass (). getresourceasstream ("OK .jpg "));
- Radiobutton RB = new radiobutton ("agree ");
- RB. setgraphic (New imageview (image ));
Add the radio button to the group
A typical use of single-choice buttons is to provide several mutex options in a group.The togglegroup object provides reference for all single-choice buttons to associate with itself, and the Management Single-choice button can only be selected at a time.Example 4-3 creates a switch button group and three single-choice buttons, adds each button to the group, and specifies which button to be selected after the program starts.
Example 4-3 creating a group of radio buttons
Java code
- Final togglegroup group = new togglegroup ();
- Radiobutton P4 = new radiobutton ("home ");
- Rb1.settogglegroup (group );
- Rb1.setselected (true );
- Radiobutton Rb2 = new radiobutton ("calendar"); rb2.settogglegroup (group );
- Radiobutton Rb3 = new radiobutton ("contacts ");
- Rb3.settogglegroup (group );
After these radio buttons are added to the application content by their layout manager, the output should be similar to Figure 4-2.
Figure 4-2 three radio buttons combined in a group
Description of "Figure 4-2 three radio buttons combined in a group"
Process radio button events
When a single-choice button in the group is selected, the program will process this action. Read the code block in Example 4-4 to learn how to change the icon Based on the selected radio button.
Example 4-4 processing action for radio buttons
Java code
- Imageview image = new imageview ();
- Rb1.setuserdata ("home ");
- Rb2.setuserdata ("calendar ");
- Rb3.setuserdata ("contacts ");
- Final togglegroup group = new togglegroup ();
- Group. selectedtoggleproperty (). addlistener (
- New changelistener <toggle> ()
- {Public void changed (observablevalue <? Extends toggle> ov,
- Toggle old_toggle, toggle new_toggle)
- {If (group. getselectedtoggle ()! = NULL)
- {Final image =
- New image (getclass (). getresourceasstream (group. getselectedtoggle (). getuserdata (). tostring () + ". jpg "));
- Icon. setimage (image );}}});
For example, when Rb3 is selected,Getselectedtoggle method return"Rb3 ,"The getuserdata method returns"Contacts ". Therefore,The getresourceasstream method receives"Contacts.jpg." figure
4-1 is the output of the application.
Indicates the request focus of the radio button.
In a single-choice button group, the first button has focus by default. When you usesetSelectedAfter the method, the expected result is as shown in Figure
4-3.
Figure 4-3 default focus settings
Description of "Figure 4-3 default focus Settings"
The second button is selected, but the focus is still on the first button. UseThe requestfocus function can change the focus position. For details, seeExample
4-5.
Example 4-5 requesting focus for the second radio button
Rb2.setselected (true); rb2.requestfocus ();
In this way, the results produced by the Code are shown in Figure 4-4.
Figure 4-4 setting focus for the selected radio button
Description of "Figure 4-4 setting focus for the selected radio button"