11th back to javafx2.0 switch button togglebutton

Source: Internet
Author: User

Original address http://download.oracle.com/javafx/2.0/ui_controls/toggle-button.htm

 

 The togglebutton class represents another type of button that can be created through the javafx API.. Two or more buttons are added to a group, but only one or more buttons are selected at a time. Figure 5-1 is an application with three switch buttons in a group. This application determines the color of the rectangle Based on the switch button.

Figure 5-1 three toggle buttons


Description of "Figure 5-1 three toggle buttons"

Create toggle button

You can useCreate a switch button for any of the three constructor methods of the togglebutton class.See example
5-1.

Example 5-1 Creating toggle buttons

//A toggle button without any caption or iconToggleButton tb1 = new ToggleButton();//A toggle button with a text captionToggleButton tb2 = new ToggleButton("Press me");//A toggle button with a text caption and an iconImage image = new Image(getClass().getResourceAsStream("icon.png"));ToggleButton tb3 = new ToggleButton ("Press me", new ImageView(image));

ToggleButtonClass inheritedLabeled class. Therefore, you can add images to text headers, images, and texts. AvailableLabeledClasssetTextAndSetgraphic method to specify text and image for the switch button.

 

After defining the switch buttons in the code, you can put them into the group and specify specific actions.

Add toggle button to group

 Implementation andThe implementation of the radiobutton class is quite similar.Unlike a single button, the switch button does not require at least one button to be selected in a group at a time. That is to say, clicking the selected switch will cancel the selection, but clicking the single button in the group does not respond.

T take a moment to read the code in Example 5-2.

Example 5-2 combining toggle buttons in a group

final ToggleGroup group = new ToggleGroup();ToggleButton tb1 = new ToggleButton("Minor");tb1.setToggleGroup(group);tb1.setSelected(true);ToggleButton tb2 = new ToggleButton("Major");tb2.setToggleGroup(group);ToggleButton tb3 = new ToggleButton("Critical");tb3.setToggleGroup(group);

Example 5-2 creates three switch buttons and adds them to the switch group. Tb1 calledSetselected method, soWhen the application is opened, it is pressed. However, you can also press the minor button so that no switch button is pressed. See figure
5-2.

Figure 5-2 three toggle buttons in a group


Description of "Figure 5-2 three toggle buttons in A group"

A group of switch buttons is generally used to assign specific actions to each button. The next section explains how to use these toggle buttons to change the color of the rectangle.

Set Behavior

Togglebutton class fromNodeClass inheritedSetuserdata MethodThis method allows you to assign specific values to any selected options. In Example
In 5-3, user data specifies the color used to draw a rectangle.

 

Example 5-3 setting user data for the toggle buttons

tb1.setUserData(Color.LIGHTGREEN);tb2.setUserData(Color.LIGHTBLUE);tb3.setUserData(Color.SALMON);final Rectangle rect = new Rectangle(145, 50);final ToggleGroup group = new ToggleGroup();group.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){    public void changed(ObservableValue<? extends Toggle> ov,        Toggle toggle, Toggle new_toggle) {            if (new_toggle == null)                rect.setFill(Color.WHITE);            else                rect.setFill(                    (Color) group.getSelectedToggle().getUserData()                );         }});

The changelistener <toggle> Object checks the switches that are pressed in the group. If no switch button is pressed, the rectangle is drawn in white. If a button is pressed,getSelectedToggleAndThe getuserdata method is called consecutively to return a colored rectangle.

For example, if you press the tb2 button,Setselectedtoggle (). getuserdata () Color.LIGHTBLUEResult figure
5-3.

Figure 5-3 using toggle buttons to paint a rectangle


Description of "Figure 5-3 using toggle buttons to paint a rectangle"

View the togglebuttonsample. Java file to check the complete code of the application.

Beautify toggle button

You can apply CSS to the switch button to improve the visual effect of the application. The use of CSS in javafx 2.0 is almost the same as that in HTML, because they are all based on the same CSS specification. Example 5-4The setstyle method changes the switch button -fx-baseCSS familiarity.

Example 5-4 applying CSS styles to toggle buttons

tb1.setStyle("-fx-base: lightgreen;");tb2.setStyle("-fx-base: lightblue;");tb3.setStyle("-fx-base: salmon;");

See Figure 5-4.

Figure 5-4 painted toggle buttons


Description of "Figure 5-4 painted toggle buttons"

You can tryOther CSS attributes of the togglebutton class, or apply animations, conversions,Various visual effects in javafx API.

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.