@Override public void start(Stage primaryStage) throws Exception { JFXComboBox<Label> combo = new JFXComboBox<>(); combo.getItems().add(new Label("Java 1.8")); combo.getItems().add(new Label("Java 1.7")); combo.getItems().add(new Label("Java 1.6")); combo.getItems().add(new Label("Java 1.5")); combo.setEditable(true); combo.setPromptText("Select Java Version"); combo.setConverter(new StringConverter<Label>() { @Override public String toString(Label object) { return object==null? "" : object.getText(); } @Override public Label fromString(String string) { return new Label(string); } }); HBox pane = new HBox(100); HBox.setMargin(combo, new Insets(20)); pane.setStyle("-fx-background-color:WHITE"); pane.getChildren().add(combo); final Scene scene = new Scene(pane, 300, 300); scene.getStylesheets().add(ComboBoxDemo.class.getResource("/css/jfoenix-components.css").toExternalForm()); primaryStage.setTitle("JFX ComboBox Demo"); primaryStage.setScene(scene); primaryStage.setResizable(false); primaryStage.show(); }
/** * init fxml when loaded. */ @PostConstruct public void init() { jfxComboBox.focusedProperty().addListener((o, oldVal, newVal) -> { if (!newVal) { jfxComboBox.validate(); } }); ChangeListener<? super Boolean> comboBoxFocus = (o, oldVal, newVal) -> { if (!newVal) { jfxEditableComboBox.validate(); } }; jfxEditableComboBox.focusedProperty().addListener(comboBoxFocus); jfxEditableComboBox.getEditor().focusedProperty().addListener(comboBoxFocus); jfxEditableComboBox.setConverter(new StringConverter<Label>() { @Override public String toString(Label object) { return object==null? "" : object.getText(); } @Override public Label fromString(String string) { return string == null || string.isEmpty() ? null : new Label(string); } }); }