-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