package graphics.api.control.choicebox; import javafx.event.EventHandler; import javafx.beans.*; import javafx.beans.property.*; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.geometry.*; import javafx.scene.Group; import javafx.beans.property.IntegerProperty; import javafx.event.ActionEvent; import javafx.collections.ObservableList; public class ChoiceBoxSeparatorGettingSelected extends Application { public static void main(String[] args) { ChoiceBoxSeparatorGettingSelected.launch(args); } private ChoiceBox testCB; private ChoiceBox itemCB = new ChoiceBox(); final IntegerProperty INDEXTOADD = new SimpleIntegerProperty(0); private ObservableList items = getItems(); Button button = new Button("Add"); HBox hbox = new HBox(); @Override public void start(Stage stage) throws Exception { float h = 400; float w = 400; stage.setHeight(h); stage.setWidth(w); stage.setTitle("ChoiceBox Test"); Scene scene = new Scene(new Group()); VBox vbox = new VBox(15.0F); testCB = new ChoiceBox(); testCB.getItems().clear(); testCB.getItems().addAll(items); button.setDisable(itemCB.getSelectionModel().getSelectedIndex() == -1); button.setOnAction(new EventHandler() { public void handle(ActionEvent e) { if (itemCB.getSelectionModel().getSelectedIndex() >= testCB.getItems().size()) { testCB.getItems().add("NewItem" + INDEXTOADD.getValue()); } else { if (testCB.getItems().size() == 0) { testCB.getItems().add("NewItem" + INDEXTOADD.getValue()); } testCB.getItems().add(itemCB.getSelectionModel().getSelectedIndex(), "NewItem" + INDEXTOADD.getValue()); } INDEXTOADD.setValue(INDEXTOADD.getValue() + 1); } }); itemCB.getSelectionModel().selectedItemProperty().addListener(new InvalidationListener() { public void invalidated(Observable ov) { button.setDisable(itemCB.getSelectionModel().getSelectedIndex() == -1); } }); ObservableList strings = FXCollections.observableArrayList(); for (int i = 0; i <= items.size(); i++) { strings.add("" + i); } itemCB.getItems().clear(); itemCB.getItems().addAll(strings); hbox.getChildren().clear(); hbox.getChildren().addAll(button, new Label("NewItem" + INDEXTOADD.getValue() + " at"), itemCB); HBox hbox2 = new HBox(); ObservableList strings2 = FXCollections.observableArrayList(); for (int i = 0; i <= items.size(); i++) { strings2.add("" + i); } vbox.getChildren().clear(); vbox.getChildren().addAll(testCB, hbox, hbox2); ((Group) scene.getRoot()).getChildren().clear(); ((Group) scene.getRoot()).getChildren().addAll(vbox); stage.setScene(scene); stage.show(); } private ObservableList getItems() { ObservableList objects = FXCollections.observableArrayList(); Separator separator = new Separator(); separator.setValignment(VPos.TOP); Separator separator2 = new Separator(); separator2.setValignment(VPos.BASELINE); Separator separator3 = new Separator(); separator3.setValignment(VPos.BOTTOM); Separator separator4 = new Separator(); separator4.setValignment(VPos.CENTER); Separator separator5 = new Separator(); separator5.setValignment(VPos.BOTTOM);//VPos.PAGE_END); objects.addAll("Zero", separator, "Two", "Three", "Four", separator2, "Six", "Seven", "Eight", separator3, "Ten", "Eleven", "Tweleve", separator4, "Fourteen", "Fifteen", "Sixteen", separator5); return objects; } }