-1
我想在JavaFX中動態創建一個GridPane視圖。問題是當試圖將孩子添加到窗格時,我得到一個「重複的孩子」異常。創建GridPane子項時出錯
這是我如何加入孩子們的窗格:
GridPane gridView = new GridPane();
for(int i = 0; i < mobster.getActions().size(); i++) {
MobsterVisualWrapper actionWrapper = new MobsterVisualWrapper(mobster.getActionList().get(i));
gridView.add(actionWrapper.getNode(), 0, i);
gridView.add(actionWrapper.getLabel(), 1, i);
}
Platform.runLater(() -> {
queuedActionsBorderPane.setCenter(gridView);
});
public static class MobsterVisualWrapper{
private final Label actionLabel;
private final ProgressBar progressBar = new ProgressBar(-1.0f);
private final ImageView imageView = new ImageView();
private Node node;
public MobsterVisualWrapper(AbstractAction action) {
actionLabel = new Label(action.getName());
actionLabel.setDisable(true);
node = new Label("-");
action.setVisualWrapper(this);
}
public void setRunning(boolean running) {
if(running) {
setNode(progressBar);
} else {
setNode(new ImageView());
}
}
public void setFailed() {
imageView.setId("actionViewFailed");
setNode(imageView);
}
public void setComplete() {
imageView.setId("actionViewComplete");
setNode(imageView);
}
/**
* Sets the node that will show the current status of the action
* @param node the node to show the status
*/
private void setNode(Node node) {
this.node = node;
}
/**
* @return the node to show the status of the action
*/
private Node getNode() {
return node;
}
public Label getLabel() {
return actionLabel;
}
}
我想添加一個新行每次循環迭代有兩列。
感謝您的高級幫助!
錯誤似乎不是由您發佈的代碼引起的(因爲您顯然只是在循環的每次迭代中添加了兩個標籤,您剛剛創建了這些標籤)。檢查異常是否從您認爲的位置拋出,並根據需要添加日誌記錄以確定將哪個節點添加到場景圖兩次。如果這樣不能解決問題,您可能需要提供一個[MCVE](強調*最小* **和** *完整*)。 –
我猜'action.setVisualWrapper(this)'看起來有點可疑。那個有什麼用?您是否使用它在稍後階段將UI組件添加到其他位置? –
將整個堆棧跟蹤添加到您的問題將使這更容易分析。 – VGR