Issue
I am trying to dynamically create a GridPane view in JavaFX. The issue is when trying to add the children to the pane I get a "duplicate children" exception.
Here is how I adding the children to the pane:
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;
}
}
I am trying to add a new row each loop iteration with two columns.
Solution
This code actually does work and no longer throws the exception. I think before I was setting GridPane.setConstraints along with the add method which may have caused some issues. I thought the issue was still persisting despite removing this but I guess that isn't the case. Thanks to everyone who tried to help.
Answered By - Adrian Elder
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.