Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.ListChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.TreeItemPropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.List;
public class TreeTableItemSelectionCausesNPE extends Application {
// This example will cause a NullPointerException when a TreeItem is
// selected. Steps to reproduce:
//
// 1. Run the example.
// 2. Click on the "one" item to select it.
//
// The NPE does not happen using 8u5 or 8u20-b17. Note that selecting the
// "one" item via code does not cause the NPE. It must be selected via
// mouse click.
@Override
public void start(final Stage primaryStage) throws Exception {
// table items - 3 items, 2nd item has 2 children
TreeItem<Item> root = new TreeItem<>();
// item one
TreeItem<Item> one = new TreeItem<>(new Item("one"));
root.getChildren().add(one);
// item two
TreeItem<Item> two = new TreeItem<>(new Item("two"));
two.getChildren().add(new TreeItem<>(new Item("childOne")));
two.getChildren().add(new TreeItem<>(new Item("childTwo")));
root.getChildren().add(two);
// item three
root.getChildren().add(new TreeItem<>(new Item("three")));
// table columns - 1 column; name
TreeTableColumn<Item, String> nameColumn = new TreeTableColumn<>("name");
nameColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("name"));
nameColumn.setPrefWidth(200);
// table
TreeTableView<Item> table = new TreeTableView<>();
table.setShowRoot(false);
table.setRoot(root);
table.getColumns().addAll(nameColumn);
table.getSelectionModel().getSelectedItems().addListener(this::processListChange);
// version / info labels
Label osLabel = new Label(System.getProperty("os.name"));
Label jvmLabel = new Label(
System.getProperty("java.version") +
"-" + System.getProperty("java.vm.version") +
" (" + System.getProperty("os.arch") + ")"
);
// scene /stage
primaryStage.setScene(new Scene(
new BorderPane(
table,
null,
null,
new VBox(osLabel, jvmLabel),
null
)
));
primaryStage.setWidth(600);
primaryStage.setHeight(400);
primaryStage.setTitle("SelectedTreeTableItems");
primaryStage.show();
table.getSortOrder().add(nameColumn);
nameColumn.setSortType(TreeTableColumn.SortType.DESCENDING);
table.getSelectionModel().select(one);
two.setExpanded(true);
// this works
//table.getSelectionModel().select(4);
// this works
//table.getSelectionModel().select(one);
}
private void processListChange(ListChangeListener.Change<? extends TreeItem<Item>> c) {
System.out.println("Change event...");
while (c.next()) {
System.out.println(" Change:");
// ignore everything but added
if (c.wasAdded()) {
List<? extends TreeItem<Item>> addedList = c.getAddedSubList();
addedList.forEach(item -> {
if(item == null) {
System.out.println(" WARNING: Added item was null.");
}
else {
System.out.println(" Added: " + item.getValue().getName());
}
});
}
}
}
public static class Item {
private final StringProperty name = new SimpleStringProperty("name");
public final ReadOnlyStringProperty nameProperty() {return name;}
public final String getName() {return name.get();}
public Item(String name) {
this.name.set(name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment