Search code examples
javafxdynamicjavafx-8

Can nodes be added to an HBox during runtime?


I have a JavaFX application in which displays information that is received from a node.js server. At one point, when it receives a specific string "new_game", it should reload 4 VBoxes which are inside of an HBox. Is it possible to remove the old VBoxes and put in new ones after the program has already launched?

(FYI, I am asking this because updating the VBoxes would be kind of a hassle because of my nooby code.)


Solution

  • So you want to remove the old vBoxes and add new one's I would use some of this:

    hBox.getChildren().clear();
    

    would remove everthing of this hbox so if you only want to remove the 4 specific vboxes then use this:

    hBox.getChildren().remove(vBox1, vBox2, vBox3, vBox4);
    

    then you want to add the new vboxes, do it like this

    hBox.getChildren().addAll(newVBox1, newVBox2, newVBox3, newVBox4);
    

    I hope this answers your question