package comboboxapp1; import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.event.Event; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Issue3 extends Application { public static void main(String[] args) { launch(args); } final ComboBox testedComboBox = new ComboBox(); @Override public void start(Stage stage) throws Exception { Pane pane = new Pane(); pane.setPrefHeight(200); pane.setPrefWidth(200); pane.getChildren().add(testedComboBox); testedComboBox.setVisibleRowCount(5); testedComboBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener() { public void changed(ObservableValue ov, Number t, Number t1) { System.out.println("Selected index " + testedComboBox.getSelectionModel().getSelectedIndex()); } }); testedComboBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { public void changed(ObservableValue ov, String t, String t1) { System.out.println("Selected item " + t1); } }); Button setNewItemList = new Button("set new items list"); setNewItemList.setOnAction(new EventHandler() { public void handle(ActionEvent t) { testedComboBox.setItems(FXCollections.observableArrayList()); } }); VBox vb = new VBox(); vb.getChildren().addAll(pane, getAddItemHBox(), setNewItemList); Scene scene = new Scene(vb, 400, 400); stage.setScene(scene); stage.show(); } private HBox getAddItemHBox() { HBox hb = new HBox(); Label lb = new Label("Add item"); final TextField tf = TextFieldBuilder.create().prefWidth(50).build(); Label atLb = new Label("at pos"); final TextField tfPos = TextFieldBuilder.create().prefWidth(50).build(); Button bt = ButtonBuilder.create().text("Add!").build(); bt.setOnAction(new EventHandler() { public void handle(Event t) { int index = Integer.parseInt(tfPos.getText()); ((ComboBox) testedComboBox).getItems().add(index, tf.getText()); } }); hb.getChildren().addAll(lb, tf, atLb, tfPos, bt); return hb; } }