Skip to content

Instantly share code, notes, and snippets.

@wcmatthysen
Created November 21, 2019 19:04
Show Gist options
  • Save wcmatthysen/76fa6751471b2428751b58ece9c6377b to your computer and use it in GitHub Desktop.
Save wcmatthysen/76fa6751471b2428751b58ece9c6377b to your computer and use it in GitHub Desktop.
TreeView Test Application
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class TreeViewTest extends Application {
public static class Person {
private final StringProperty nameProperty;
private final StringProperty surnameProperty;
public Person() {
this.nameProperty = new SimpleStringProperty();
this.surnameProperty = new SimpleStringProperty();
}
public StringProperty nameProperty() {
return this.nameProperty;
}
public void setName(String value) {
this.nameProperty.set(value);
}
public String getName() {
return this.nameProperty.get();
}
public StringProperty surnameProperty() {
return this.surnameProperty;
}
public void setSurname(String value) {
this.surnameProperty.set(value);
}
public String getSurname() {
return this.surnameProperty.get();
}
@Override
public String toString() {
return getName() + " " + getSurname();
}
}
public static void expandDescendants(TreeItem<?> treeItem) {
List<? extends TreeItem<?>> children = treeItem.getChildren();
if (!children.isEmpty()) {
for (TreeItem<?> child : children) {
child.setExpanded(true);
expandDescendants(child);
}
}
}
@Override
public void start(Stage primaryStage) throws Exception {
TreeView<Person> treeView = new TreeView<>();
TreeItemSelectionFilter<Person> filter = TreeItem::isLeaf;
MultipleSelectionModel<TreeItem<Person>> selectionModel = treeView.getSelectionModel();
selectionModel.setSelectionMode(SelectionMode.SINGLE);
FilteredTreeViewSelectionModel<Person> filteredSelectionModel = new FilteredTreeViewSelectionModel<>(treeView, treeView.getSelectionModel(), filter);
treeView.setSelectionModel(filteredSelectionModel);
treeView.setEditable(true);
TreeItem<Person> rootItem = new TreeItem<>();
treeView.setRoot(rootItem);
treeView.setShowRoot(false);
List<TreeItem<Person>> rootChildren = rootItem.getChildren();
Person john = new Person();
john.setName("John");
john.setSurname("Denver");
TreeItem<Person> johnTreeItem = new TreeItem<>(john);
rootChildren.add(johnTreeItem);
List<TreeItem<Person>> johnChildren = johnTreeItem.getChildren();
Person peter = new Person();
peter.setName("Peter");
peter.setSurname("Denver");
TreeItem<Person> peterTreeItem = new TreeItem<>(peter);
johnChildren.add(peterTreeItem);
Person daisy = new Person();
daisy.setName("Daisy");
daisy.setSurname("Denver");
TreeItem<Person> daisyTreeItem = new TreeItem<>(daisy);
johnChildren.add(daisyTreeItem);
Person edward = new Person();
edward.setName("Edward");
edward.setSurname("Johnson");
TreeItem<Person> edwardTreeItem = new TreeItem<>(edward);
rootChildren.add(edwardTreeItem);
List<TreeItem<Person>> edwardChildren = edwardTreeItem.getChildren();
Person andrew = new Person();
andrew.setName("Andrew");
andrew.setSurname("Johnson");
TreeItem<Person> andrewTreeItem = new TreeItem<>(andrew);
edwardChildren.add(andrewTreeItem);
Person michelle = new Person();
michelle.setName("Michelle");
michelle.setSurname("Johnson");
TreeItem<Person> michelleTreeItem = new TreeItem<>(michelle);
edwardChildren.add(michelleTreeItem);
expandDescendants(rootItem);
MultipleSelectionModel<TreeItem<Person>> currentSelectionModel = treeView.getSelectionModel();
currentSelectionModel.clearSelection();
BorderPane mainPane = new BorderPane();
mainPane.setCenter(treeView);
FlowPane buttonPane = new FlowPane();
List<Node> buttons = buttonPane.getChildren();
Button firstButton = new Button("First");
firstButton.setOnAction(e -> currentSelectionModel.selectFirst());
buttons.add(firstButton);
Button lastButton = new Button("Last");
lastButton.setOnAction(e -> currentSelectionModel.selectLast());
buttons.add(lastButton);
Button nextButton = new Button("Next");
nextButton.setOnAction(e -> currentSelectionModel.selectNext());
buttons.add(nextButton);
Button previousButton = new Button("Previous");
previousButton.setOnAction(e -> currentSelectionModel.selectPrevious());
buttons.add(previousButton);
Button clearButton = new Button("Clear");
clearButton.setOnAction(e -> currentSelectionModel.clearSelection());
buttons.add(clearButton);
mainPane.setBottom(buttonPane);
Scene scene = new Scene(mainPane, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment