Managing ListView items

The ListView content is backed up by an ObservableList and any changes to this list are automatically reflected in the ListView, as in the button handler in the next example:

// chaptep10/list/ListViewDemo.javaObservableList<String> items = FXCollections.observableArrayList(        "Red", "Blue", "Yellow", "Green");ListView<String> list = new ListView<>(items);Button btn = new Button("Add New Item");btn.setOnAction((e) -> {    items.add(            // just a way to generate semirandom new items            items.get(new Random().nextInt(items.size()))    );});

Get Mastering JavaFX 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.