Tuesday, January 7, 2020

JavaFX Controls ChoiceBox Overview

TheChoiceBoxclass is used to create a control which presents the user with a few choices to pick from a drop-down list. The user is only allowed to pick one of the options. When the drop-down list is not showing then the currently selected option is the only one visible. It is possible to set the ChoiceBox object to accept a null option as a valid choice. Import Statement import javafx.scene.control.ChoiceBox; Constructors TheChoiceBox class has two constructors one for an empty list of items and one with a given set of items: //Create an empty ChoiceBoxChoiceBox choices new ChoiceBox();//Create a ChoiceBox using an observable list collectionChoiceBox cboices new ChoiceBox(FXCollections.observableArrayList(Apple, Banana, Orange, Peach, Pear, Strawberry)); Useful Methods If you choose to create an emptyChoiceBox items can be added later using the setItems method: choices.setItems(FXCollections.observableArrayList(Apple, Banana, Orange, Peach, Pear, Strawberry)); And, if you want to find out what items are in aChoiceBox you can use the getItems method: List options choices.getItems(); To pick an option to be currently selected use thesetValue method and provide it with one of the options: choices.setValue(First); To get the value of the option currently selected use the correspondinggetValue method and assign it to a String: String option choices.getValue().toString(); Event Handling In order to listen to events for aChoiceBox object, the SelectionModel is used. The ChoiceBox uses the SingleSelectionModel class which only permits one option to be chosen at a time. The selectedIndexProperty method allows us to add a ChangeListener. This means that whenever the option selected changes to another option the change event will occur. As you can see from the code below, a change is listened for and when it occurs the previously selected option and the newly selected option can be determined: final List options choices.getItems();choices.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener() { Override public void changed(ObservableValue ov, Number oldSelected, Number newSelected) { System.out.println(Old Selected Option: options.get(oldSelected.intValue())); System.out.println(New Selected Option: options.get(newSelected.intValue())); } }); Its also possible to show or hide the list of options without the user having to click on theChoiceBox object by using the show and hide methods. In the code below a Button object is used to call the show method of a ChoiceBox object when the Button is clicked: //Use a stackpane for a simple layout of the controlsStackPane root new StackPane();//Create Button to show the options in the ChoiceBoxButton showOptionButton new Button(Show Options);root.getChildren().add(showOptionButton);root.setAlignment(showOptionButton, Pos.TOP_CENTER);//Create the ChoiceBox with a few optionsfinal ChoiceBox choices new ChoiceBox(FXCollections.observableArrayList(Apple, Banana, Orange, Peach, Pear, Strawberry));root.getChildren().add(choices);//Use the ActionEvent to call the ChoiceBox show methodshowOptionButton.setOnAction(new EventHandler() { Override public void handle(ActionEvent e) { choices.show(); }});//Set the Scene and put the Stage into motion..Scene scene new Scene(root, 300, 250);primaryStage.setScene(scene);primaryStage.show(); To find out about other JavaFX controls, have a look at JavaFX User Interface Controls.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.