package helloworld; import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javafx.scene.control.ComboBox; import javafx.scene.control.TextField; public class HelloComboBoxJc extends Application { private final ObservableList strings = FXCollections.observableArrayList( "Option 1", "Option 2", "Option 3"); private int nb = 0; public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { stage.setTitle("ComboBox"); final TextField textField = new TextField("test"); textField.setOnAction(new EventHandler() { public void handle(ActionEvent t) { try { double val = Double.parseDouble(textField.getText()); if (val > 10) { textField.setText("too big !"); } } catch (Exception ex) { // not a number } } }); // textField.focusedProperty().addListener(new ChangeListener() { // // public void changed(ObservableValue ov, Boolean oldVal, Boolean newVal) { // if (!newVal) { // textField.selectAll(); // } // } // }); // textField.textProperty().addListener(new ChangeListener() { // // public void changed(ObservableValue ov, String t, String t1) { // if (t1.endsWith("end")) { // textField.selectAll(); // } // } // }); final ComboBox comboBox1 = new ComboBox(); comboBox1.setId("comboBox-editable"); comboBox1.setItems(strings); comboBox1.setEditable(true); comboBox1.setPrefWidth(100); comboBox1.setValue(strings.get(0)); comboBox1.setPromptText("Empty"); // TextField comboBoxEditor = comboBox1.getEditor(); // comboBoxEditor.setId("editor"); // comboBoxEditor.setText("Init value"); // comboBoxEditor.setPromptText("Empty"); // comboBox1.valueProperty().addListener(new ChangeListener() { // // @Override // public void changed(ObservableValue ov, String t, String t1) { // System.out.println("new value: " + t1); // } // }); comboBox1.setOnAction(new EventHandler() { public void handle(ActionEvent t) { System.out.println("New value (from getEditor) : " + comboBox1.getEditor().getText()); System.out.println("New value (from getValue) : " + comboBox1.getValue()); try { double val = Double.parseDouble(comboBox1.getValue()); if (val > 10) { comboBox1.setValue("too big !"); } } catch (Exception ex) { // not a number } } }); Button changeButton = new Button("Set value"); changeButton.setOnAction(new EventHandler() { public void handle(ActionEvent t) { comboBox1.setValue("" + nb); // comboBox1.getEditor().setText("" + nb++); // comboBox1.getEditor().selectAll(); textField.setText("" + nb); // textField.selectAll(); nb++; } }); Button prompTextButton = new Button("Set promptText"); prompTextButton.setOnAction(new EventHandler() { public void handle(ActionEvent t) { comboBox1.setPromptText("Enter a value"); textField.setPromptText("Enter a value"); } }); Button selectButton = new Button("Select"); selectButton.setOnAction(new EventHandler() { public void handle(ActionEvent t) { comboBox1.getEditor().selectAll(); textField.selectAll(); } }); VBox vbox = new VBox(20); vbox.setLayoutX(40); vbox.setLayoutY(25); vbox.getChildren().addAll(textField, comboBox1, changeButton, prompTextButton, selectButton); Scene scene = new Scene(new Group(vbox), 620, 400); stage.setScene(scene); stage.show(); } }