package javafxsamples; import javafx.application.Application; import javafx.builders.ButtonBuilder; import javafx.builders.VBoxBuilder; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ListView; import javafx.scene.layout.VBox; import javafx.stage.Stage; /** * Test for listView selection * @author Alex */ public class ListViewTest extends Application{ @Override public void start(Stage primaryStage) throws Exception { final ListView listView = new ListView(); listView.getItems().add("test"); listView.getSelectionModel().select(0); final Button button = new ButtonBuilder() .text("empty") .onAction(new EventHandler() { @Override public void handle(ActionEvent event) { listView.getItems().clear(); listView.getItems().add("test"); } }).build(); VBox box = new VBoxBuilder() .children(button, listView) .build(); primaryStage.setScene(new Scene(box, 200, 100)); primaryStage.setVisible(true); } public static void main(String[] args) { launch(args); } }