13th back to javafx2.0 option box choicebox

Source: Internet
Author: User

This article is from my translationArticleHttp://somefuture.iteye.com/blog/1202111

Original address http://download.oracle.com/javafx/2.0/ui_controls/choice-box.htm#BCEDJAEH

 

The choicebox class supports quick selection among several options. LookFigure
What is the simple implementation of the Option box in 7-1.

Figure 7-1 Creating a choice box with three items


Description of "Figure 7-1 Creating a choice box with three items"

Create choice box

Example 7-1 creates an option box with three entries.

Example 7-1 Creating a choice box

 
Choicebox cb = new choicebox (fxcollections. observablearraylist ("first", "second", "third "));

Example 7-1 is created inThe list of entries in the constructor of the choicebox class. This list of entries is specified with a visible array. Of course, you can choose to use the null constructor of this class, and then useSet the list of entries in the setitems method. SeeExample
7-2.

Example 7-2 choice box with text elements and a separator

 
Choicebox cb = new choicebox (); CB. setitems (fxcollections. observablearraylist ("New Document", "open", new separator (), "save", "Save "));

However, the option box can not only contain text elements, but also other objects. Example 7-2The separator control separates entries. When these are added to the applicationCodeFor more information, seeFigure
7-2.

Figure 7-2 menu created by using a choice box


Description of "Figure 7-2 menu created by using a choice box"

In a project, the option box is used to create a multiple-choice list.

Set behavior for choice box

The application in Figure 7-3 provides a multi-choice box with five options. When a specific language is selected, the corresponding greetings are displayed.

Figure 7-3 multiple-choice list


Description of "Figure 7-3 multiple-choice list"

The code block in Figure 7-4 explains how to define which language greeting should be displayed after an entry in the option box is selected.

Figure 7-4 selecting a choice box item


Description of "Figure 7-4 selecting a choice box item"

Through continuous calls Getselectionmodel And Selectedindexproperty method, Changelistener <number> The object detects the index of the selected entry.The getselectionmodel method returns the selected entries, The selectedindexproperty method returns Selected_index attribute. In this way, the integer value of the index defines the elements in the greeting array, And specify String Text. If a user selects the second entry, it corresponds to Spanish, Selected_index is 1, "Hola" is selected from the greeting number. In this way, the label displays "Hola ."

You can useThe choicebox control assigns a prompt to make it more instructive. Note: YesA ui control in the javafx. Scene. Control package. The prompt bar can be used for any javafx
UI control.

Use tooltip

The tooltip class provides a pre-fabricated Method for adding a prompt bar in the option box (and all other controls). You can callThe settooltip method is easy to use. SeeExample
7-3.

Example 7-3 adding a tooltip to a choice box

 
CB. settooltip (New tooltip ("select the language "));

GenerallyThe prompt content is defined in the constructor of The tooltip class. However, if your application logic requires the UI to dynamically set text, you can use it using the empty constructor and thenThe settext method sets text for it.

After a prompt bar is used in the option box, you can place the cursor over the option box to see an image bar. See Figure 7-5.

Figure 7-5 choice box with the applied tooltip


Description of "Figure 7-5 choice box with the applied tooltip"

To improve the appearance of an application, you can use CSS in the option box, or use visual effects and transformations.

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.