10. javafx2.0 single-threaded radio button

Source: Internet
Author: User

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
  1. // A radio button with an empty string for its label
  2. Radiobutton P4 = new radiobutton ();
  3. // Setting a text label
  4. Rb1.settext ("home ");
  5. // A radio button with the specified label
  6. 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
  1. Image image = new image (getclass (). getresourceasstream ("OK .jpg "));
  2. Radiobutton RB = new radiobutton ("agree ");
  3. 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
  1. Final togglegroup group = new togglegroup ();
  2. Radiobutton P4 = new radiobutton ("home ");
  3. Rb1.settogglegroup (group );
  4. Rb1.setselected (true );
  5. Radiobutton Rb2 = new radiobutton ("calendar"); rb2.settogglegroup (group );
  6. Radiobutton Rb3 = new radiobutton ("contacts ");
  7. 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
  1. Imageview image = new imageview ();
  2. Rb1.setuserdata ("home ");
  3. Rb2.setuserdata ("calendar ");
  4. Rb3.setuserdata ("contacts ");
  5. Final togglegroup group = new togglegroup ();
  6. Group. selectedtoggleproperty (). addlistener (
  7. New changelistener <toggle> ()
  8. {Public void changed (observablevalue <? Extends toggle> ov,
  9. Toggle old_toggle, toggle new_toggle)
  10. {If (group. getselectedtoggle ()! = NULL)
  11. {Final image =
  12. New image (getclass (). getresourceasstream (group. getselectedtoggle (). getuserdata (). tostring () + ". jpg "));
  13. 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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.