import javafx.application.Application; import javafx.beans.InvalidationListener; import javafx.beans.Observable; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.layout.StackPane; import javafx.stage.Stage; /** * * @author jfdenise */ public class TestComboBox extends Application { /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); final ComboBox cb = new ComboBox(); cb.showingProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue arg0, Boolean oldValue, Boolean newValue) { if (newValue) { cb.getItems().setAll("" + System.currentTimeMillis()); } } }); cb.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { System.out.println("OnAction called: " + cb.getValue()); } }); System.out.println("initial index: " + cb.getSelectionModel().getSelectedIndex()); System.out.println("initial item: " + cb.getSelectionModel().getSelectedItem()); cb.getSelectionModel().selectedIndexProperty().addListener(new InvalidationListener() { public void invalidated(Observable arg0) { System.out.println("new index: " + cb.getSelectionModel().getSelectedIndex()); } }); cb.getSelectionModel().selectedItemProperty().addListener(new InvalidationListener() { public void invalidated(Observable arg0) { System.out.println("new item: " + cb.getSelectionModel().getSelectedItem()); } }); cb.getItems().add("Toto"); cb.setEditable(true); cb.setValue("Tata"); StackPane root = new StackPane(); root.getChildren().add(cb); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } }