/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tests; import javafx.application.Application; import javafx.beans.InvalidationListener; import javafx.beans.Observable; import javafx.collections.ListChangeListener; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.SelectionMode; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; import javafx.scene.input.KeyEvent; import javafx.stage.Stage; /** * * @author sjiang */ public class MyTreeViewTest2 extends Application { public static void main(String[] args) throws Exception { Application.launch(args); } @Override public void start(Stage stage) throws Exception { final TreeView treeView1 = new TreeView(); treeView1.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); treeView1.setPrefSize(300, 300); TreeItem root1 = new TreeItem("Root"); root1.setExpanded(true); addChildren(root1, "child"); for (TreeItem child : root1.getChildren()) { addChildren(child, (String)child.getValue()); child.setExpanded(true); } treeView1.setRoot(root1); treeView1.getSelectionModel().getSelectedItems().addListener(new InvalidationListener() { public void invalidated(Observable observable) { System.out.println("\n---jsl-InvalidationListener observable="+observable); System.out.println("---jsl-InvalidationListener getSelectedItems="+treeView1.getSelectionModel().getSelectedItems()); } }); treeView1.getSelectionModel().getSelectedItems().addListener(new ListChangeListener() { public void onChanged(Change c) { System.out.println("---jsl-ListChangeListener getSelectedItems="+treeView1.getSelectionModel().getSelectedItems()); System.out.println("---jsl-ListChangeListener c.getList()"+c.getList()); // System.out.println("---jsl-ListChangeListener c.getAddedSubList()="+c.getAddedSubList()); // System.out.println("---jsl-ListChangeListener c.getRemoved()="+c.getRemoved()); } }); // treeView1.addEventHandler(KeyEvent.KEY_TYPED, new EventHandler() { // public void handle(KeyEvent event) { // treeView1.getSelectionModel().select(0); // treeView1.getSelectionModel().select(treeView1.getTreeItem(1)); // System.out.println("---jsl-EventHandler "+treeView1.getSelectionModel().getSelectedItems()); // } // // }); // HBox hbox = new HBox(); // hbox.getChildren().setAll(treeView1); Scene scene = new Scene(treeView1); stage.setScene(scene); stage.show(); } private void addChildren(TreeItem parent, String name) { for (int i=0; i<3; i++) { TreeItem ti = new TreeItem(name+"-"+i); parent.getChildren().add(ti); } } }