- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Example to set action listeners (behavior) to a ChoiceBox in JavaFX
A choice box holds a small set of multiple choices and, allows you to select only one of them. This will have two states showing and not showing. When showing, you can see the list of choices in a choice box and while not showing, it displays the current choice. By default, no option is selected in a choice box.
In JavaFX a choice box is represented by the class javafx.scene.control.ChoiceBox<T>. You can create a choice box by instantiating this class.
The selectionModel property of this class holds the selection model of the current choice box, you can get the value of it using the getSelectionModel() method (which is always SingleSelectionModel).
The SelectionModel class has two properties namely selectedIndex and selectedItem specifying the selected index and selected items respectively. To perform an action when an item is selected you can do so by adding action listener on to either of these properties using the addListener() method as −
getSelectionModel().selectedIndexProperty().addListener() or, getSelectionModel().selectedItemProperty().addListener()
Example
import javafx.application.Application; import javafx.beans.value.ObservableValue; import javafx.collections.ObservableList; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage; public class ChoiceBoxAction extends Application { public void start(Stage stage) { //Creating a choice box ChoiceBox<String> choiceBox = new ChoiceBox<String>(); //Retrieving the observable list ObservableList<String> list = choiceBox.getItems(); //Adding items to the list list.add("English"); list.add("Hindi"); list.add("Telugu"); //Setting the position of the choice box choiceBox.setTranslateX(200); choiceBox.setTranslateY(15); //Setting the label Label label = new Label("Select Display Language:"); Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12); label.setFont(font); label.setTranslateX(20); label.setTranslateY(20); //Setting the text for message Text text = new Text(25.0, 130.0, ""); text.setFont(Font.font("Britannic Bold", 33)); text.setFill(Color.BROWN); String language[] = new String[]{ "Welcome to Tutorilaspoint", "टुटोरियल्स पॉइन्ट को आप का स्वागत है", "ట్యూ టోరియల్స్ పాయింట్ కి స్వా గతిం" }; //Adding action to the choice box choiceBox.getSelectionModel().selectedIndexProperty().addListener( (ObservableValue<? extends Number> ov, Number old_val, Number new_val) -> { text.setText(language[new_val.intValue()]); }); //Adding the choice box to the group Group root = new Group(choiceBox, label, text); //Setting the stage Scene scene = new Scene(root, 595, 170, Color.BEIGE); stage.setTitle("Choice Box Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
When selected English −
When selected Hindi −
When selected Telugu −
- Related Articles
- JavaFX example to set action listeners to a CheckBox
- JavaFX example to set action (behavior) to the menu item
- JavaFX example to set behavior to a button
- How to add action listeners to ContextMenu in JavaFX?
- JavaFX example to set action to the "exit" MenuItem
- How to set action to a RadioButton in JavaFX?
- How to set action to a slider using JavaFX?
- JavaFX example to set slider to the progress bar
- How to add an image to a button (action) in JavaFX?
- JavaFX example to add a tooltip to a radio button
- JavaFX example to apply multiple transformations on a node
- How to set mnemonics in a label using JavaFX?
- How to set a background image to a JavaFX chart?
- How to set action command to JButton in Java
- How to set font to text node in JavaFX?
