1. TreeView Node Addition Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | void initTree() { TreeItem<String> root = new TreeItem<> ("Root"); TreeItem<String> nodeA = new TreeItem<>("Item A"); TreeItem<String> nodeB = new TreeItem<>("Item B"); TreeItem<String> nodeC = new TreeItem<>("Item C"); root.getChildren().addAll(nodeA, nodeB, nodeC); root.setExpanded(true); TreeItem<String> nodeItemA1 = new TreeItem<>("Item A1"); TreeItem<String> nodeItemA2 = new TreeItem<>("Item A2"); TreeItem<String> nodeItemA3 = new TreeItem<>("Item A3"); nodeA.getChildren().add(nodeItemA1); nodeA.getChildren().add(nodeItemA2); nodeA.getChildren().add(nodeItemA3); nodeA.setExpanded(true); Image icon = new Image(getClass().getResourceAsStream("folder.png")); List<TreeItem<String>> nodeItemB = new ArrayList<>(); nodeItemB.add( new TreeItem<>("Item B1",new ImageView(icon))); nodeItemB.add( new TreeItem<>("Item B2",new ImageView(icon))); nodeItemB.add( new TreeItem<>("Item B3",new ImageView(icon))); nodeB.getChildren().addAll(nodeItemB); nodeB.setExpanded(true); treeView.setRoot(root); treeView.getSelectionModel().selectFirst(); treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>() { @Override public void changed(ObservableValue<? extends TreeItem<String>> observable, TreeItem<String> oldValue, TreeItem<String> newValue) { Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("JavaFXUtility"); alert.setHeaderText(null); alert.setContentText(String.format("selected=%s", newValue.getValue())); alert.showAndWait(); } }); } |
Result:
2. Node Addition with Single Item
1 2 3 4 5 6 | TreeItem<String> nodeItemA1 = new TreeItem<>("Item A1"); TreeItem<String> nodeItemA2 = new TreeItem<>("Item A2"); TreeItem<String> nodeItemA3 = new TreeItem<>("Item A3"); nodeA.getChildren().add(nodeItemA1); nodeA.getChildren().add(nodeItemA2); nodeA.getChildren().add(nodeItemA3); |
3. Node Addition with Multiple Items
1) Node Addition using variable function arguments
1 2 3 4 | TreeItem<String> nodeA = new TreeItem<>("Item A"); TreeItem<String> nodeB = new TreeItem<>("Item B"); TreeItem<String> nodeC = new TreeItem<>("Item C"); root.getChildren().addAll(nodeA, nodeB, nodeC); |
2) Node Addition using List
1 2 3 4 5 | List<TreeItem<String>> nodeItemB = new ArrayList<>(); nodeItemB.add( new TreeItem<>("Item B1",new ImageView(icon))); nodeItemB.add( new TreeItem<>("Item B2",new ImageView(icon))); nodeItemB.add( new TreeItem<>("Item B3",new ImageView(icon))); nodeB.getChildren().addAll(nodeItemB); |
4. Node Addition with Icon Image
1 2 3 | nodeItemB.add( new TreeItem<>("Item B1",new ImageView(icon))); nodeItemB.add( new TreeItem<>("Item B2",new ImageView(icon))); nodeItemB.add( new TreeItem<>("Item B3",new ImageView(icon))); |
5. Handling Node Click Event
1 2 3 4 5 6 7 8 9 10 | treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>() { @Override public void changed(ObservableValue<? extends TreeItem<String>> observable, TreeItem<String> oldValue, TreeItem<String> newValue) { Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("JavaFXUtility"); alert.setHeaderText(null); alert.setContentText(String.format("selected=%s", newValue.getValue())); alert.showAndWait(); } }); |
댓글 없음:
댓글 쓰기