[英]JavaFx - When are elements created
我試圖在實際查看屏幕之前訪問Accordian
元素,但是每次訪問它時,它都沒有子元素。 一旦我參觀了舞台,孩子們就會出現。 我已在SceneBuilder中構建了此場景,並且一旦所有組件在屏幕上可見,它就會按預期工作。
使用FXMLLoader
加載整個結構。 由於項目大小(30k +代碼行),我無法發布代碼。 我已經驗證FXMLLoader
正在運行,並且在指定的Parent
得到了所有頂級元素。 (按照預期的方式工作,直到在加載舞台之前嘗試訪問元素為止)
我試圖在應用程序的所有不同階段調用start(stage)
,以迫使它們完全加載,但是我仍然得到相同的結果。
這是獲取Node
所有子代的代碼,該Node
在屏幕上可見該階段之后就可以正常工作,直到此后才找到頂級項目,並且它們都具有0個子代。
public static ObservableList<Node> getAllChildren(Node n) {
ObservableList<Node> allChildren = FXCollections.observableArrayList();
ObservableList<Node> children = ((Parent) n).getChildrenUnmodifiable();
for (Node c : children) {
if (c instanceof Parent) {
ObservableList<Node> cp = ((Parent) c).getChildrenUnmodifiable();
if (cp.size() > 0) { // 0 for all top-level Parents until the stage has been visually loaded.
ObservableList<Node> nodes = getAllChildren(c);
for (Node node : nodes) {
allChildren.add(node);
}
}
}
allChildren.add(c);
}
return allChildren;
}
編輯:進一步的測試表明,該結構實際上是基於元素構建的(在本例中為TabPane
,我能夠使用getTabs().get(n).getContent()
並按預期方式獲得帶有其子getTabs().get(n).getContent()
的選項卡。 ,這時在執行過程中,我仍然得到TabPane
沒有子項的信息,直到顯示了階段之后才有子項。
創建skin
時,將Control
的子級添加到Contol
中,默認情況下會在第一次布局Control
時發生這種情況。
TitledPane tp = new TitledPane();
Accordion acc = new Accordion(tp);
System.out.println(acc.getChildrenUnmodifiable().size()); // 0
Scene scene = new Scene(acc);
// layout happens here
acc.applyCss();
acc.layout();
System.out.println(acc.getChildrenUnmodifiable().size()); // 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.