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. Look
Figure
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 use
Set the list of entries in the setitems method. See
Example
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: Yes
A 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 call
The settooltip method is easy to use. See
Example
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 then
The 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.