JavaFX ComboBox handle selection event
// Demonstrate a combo box. import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; public class Main extends Application { private ComboBox<String> myComboBox; private Label response = new Label();; public static void main(String[] args) { launch(args);/* w w w . j a v a2 s . co m*/ } public void start(Stage myStage) { myStage.setTitle("ComboBox Demo"); // Use a FlowPane for the root node. In this case, // vertical and horizontal gaps of 10. FlowPane rootNode = new FlowPane(10, 10); rootNode.setAlignment(Pos.CENTER); Scene myScene = new Scene(rootNode, 280, 120); myStage.setScene(myScene); // Create an ObservableList of entries for the combo box. ObservableList<String> data = FXCollections.observableArrayList("CSS", "HTML", "Java"); myComboBox = new ComboBox<String>(data); // Set the default value. myComboBox.setValue("Java"); // Set the response label to indicate the default selection. response.setText("Selected is " + myComboBox.getValue()); // Listen for action events on the combo box. myComboBox.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent ae) { response.setText("Selected is " + myComboBox.getValue()); } }); // Add the label and combo box to the scene graph. rootNode.getChildren().addAll(myComboBox, response); myStage.show(); } }