12th javafx2.0 check box checkbox

Source: Internet
Author: User

Text from my translation Article http://somefuture.iteye.com/blog/1198735

Original address http://download.oracle.com/javafx/2.0/ui_controls/checkbox.htm

 

The checkbox class allows you to create check boxes in an application. Although the check box looks like a single regionBut they cannot be placed into a switch group to achieve many options. You can review the previous content about the radio button and toggle button.

Figure 6-1 is an application with three check boxes. These check boxes are used to control whether the icons in the toolbar are displayed.

Figure 6-1 checkbox sample


Description of "Figure 6-1 checkbox sample"

 

Create a checkbox

Example 6-1 creates two simple checkboxes.

Java code
  1. // Example 6-1 Creating checkboxes
  2. // A checkbox without a caption
  3. Checkbox CB1 = new checkbox ();
  4. // A checkbox with a string Caption
  5. Checkbox CB2 = new checkbox ("second ");
  6. Cb1.settext ("first ");
  7. Cb1.setselected (true );

 

After creating the check boxes, you can use the methods in the javafx API to set them. In Example 6-1The settext method isThe check box C1 defines the title and setsThe setselected method isTrueCB1 is selected.

Define status

Check boxes can be defined as clear or ambiguous. If it is defined as clear, it can be selected or deselected. If it is not clear, it cannot be selected or deselected. Combined with the checkbox classsetSelectedAndSetindeterminate method to specify the status for the check box.Table
6-1 is three check boxes in different States.INDETERMINATEAndThe selected attributes are different.

Table 6-1 states of a checkbox

Property values Checkbox appearance

INDETERMINATE= False

SELECTED= False

INDETERMINATE= False

SELECTED= True

INDETERMINATE= True

SELECTED= True/false

Specify three States for the check boxes to display UI elements in multiple States, such as "yes", "no", "Not applicable ". Checkbox classAllowindeterminatepropety sets whether the check box object can be cyclically in all three states (selected, deselected, or ambiguous). If you setNowThis control can be cyclically in three states. OtherwiseThis control can only switch between the selected and unselected statuses. In the next part of the application, three check boxes are constructed and there are only two statuses.

 

 

Set Behavior

The code block in Example 6-2 creates three check boxes. When a check box is selected, the corresponding icons are displayed.

Java code
  1. // Example 6-2 setting the behavior for the checkboxes
  2. Final string [] names = new string [] {"security", "project", "chart "};
  3. Final image [] images = new image [names. Length];
  4. Final imageview [] icons = new imageview [names. Length];
  5. Final checkbox [] CBS = new checkbox [names. Length];
  6. For (INT I = 0; I <names. length; I ++)
  7. {Final image = images [I] =
  8. New image (getclass (). getresourceasstream (Names [I] + ". PNG "));
  9. Final imageview icon = icons [I] = new imageview ();
  10. Final checkbox cb = CBS [I] = new checkbox (Names [I]);
  11. CB. selectedproperty (). addlistener
  12. (New changelistener <Boolean> (){
  13. Public void changed (observablevalue <? Extends Boolean> ov, Boolean old_val, Boolean new_val)
  14. {Icon. setimage (new_val? Image: NULL );
  15. }});
  16. }

 

The names array usesFor loop to create check boxes and corresponding icons. For example,CBS [0] is the first check box and is assigned the "Security" title. At the same time, image [0] receives "security.png"The Object Name of the getresourcestream method. If a specific check box is selected, the corresponding image is allocated as its icon. If a check box is deselected, the icon receivesNo icons are allocated for null images.

Figure 6-2 shows that the security and chart check boxes in the application are selected, and the project is deselected.

Figure 6-2 checkbox application in action


Description of "Figure 6-2 checkbox application in action"

 

 

Beautify checkbox

The check boxes in Figure 6-2 have the default appearance and feeling of the checkbox class. You can usesetStyleMethod to change its appearance, see Example
6-3.

Java code
  1. // Example 6-3 styling a checkbox
  2. Cb1.setstyle ("-Fx-border-color: lightblue ;"
  3. + "-Fx-font-size: 20 ;"
  4. + "-Fx-border-insets:-5 ;"
  5. + "-Fx-border-radius: 5 ;"
  6. + "-Fx-border-style: dotted ;"
  7. + "-Fx-border-width: 2 ;");

 

The new style includes a light blue dot border and an expanded Font title. Figure 6-3 uses the CB1 check box of this style.

Figure 6-3 styled checkbox


Description of "Figure 6-3 styled checkbox"

To set a specific style for all the check boxes in the application, follow these steps:

  • Create a. CSS file;

  • Create in the. CSS file
    checkbox
    CSS class.

  • IncheckboxAll required styles are defined in the CSS class.

  • In your javafx application, usesetStyleClassMethod to use the CSS file.

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.