I'm currently creating an app using JavaFX, but now I need to dynamically remove or add nodes (components). However, there is almost no information to follow the node from the root node to the lower node, so we will describe the method by trial and error. (For now, I will only list the components I checked)
environment OS : macOS X Catalina Integrated development environment: Eclipse 2019-12 Java : AdoptOpenJDK 11.0.5 JavaFX : 11.0.2 SceneBuilder : 11.0
NodeTree
1:AnchorPane ← Root node ①
2: ┣━━━ MenuBar
3: ┣━┳━ AnchorPane ②
4: ┃ ┗━┳━ SplitPane ←Horizontal(Left and right split) ③
5: ┃ ┣━━━ AnchorPane
6: ┃ ┗━━━ PieChart ④
7: ┗━━━ AnchorPane
Follow the nodes in the order of ① ⇢ ② ⇢ ③ ⇢ ④.
sample.java
1: public Scene setPieChart(Stage stage) {
2: Scene scene = stage.getScene();
3: AnchorPane ap2 = (AnchorPane)this.scene.getRoot(); ①
4: AnchorPane ap3 = (AnchorPane)ap2.getChildren().get(1); ②
5: SplitPane spHorizontal = (SplitPane)ap3.getChildren().get(0); ③
6: ObservableList<?> ol = (ObservableList<?>) spHorizontal.getItems(); ③
7: PieChart pieChart = (PieChart)ol.get(1); ④
8:
9: return scene;
10: }
Note that if you try to get a child node of SplitPane, you will get an object of type ʻObservableList <?> With getItems ()
as in line 5.
In this example, in the ʻObservableList <?> Type object, the 0th element is set to
NodeTree ②AnchorPane, and the 1st element is set to
PieChart`. That's why ol.get (1) is done on the 7th line of sample.java.
One more thing to note about the operation is that if the parent node of PieChart is a container such as AnchorPane, the drawn graph will not be scaled according to the resizing of the window. Therefore, Pie Chart is placed directly under Split Pane.
Another place that might get caught is ScrollPane
.
Specifically, assuming that the child node of ScrollPane is AnchorPane, the acquisition method of AnchorPane is as follows.
sample2.java
1: AnchorPane anchorPane = (AnchorPane) scrollPane.contentProperty().get();
That's all for the time being. Also, if it becomes necessary to investigate during development, I will add it.
It also serves as a memorandum of my own, but I hope it helps everyone.
Recommended Posts