Javafx Controlling UI from separate class, can't add node from calling class

144 views Asked by At

I am experimenting with Javafx for a simple chess-like game. From my start class, I am successful in using another class, "UIBuilder", to modify "Stage primaryStage". My UIBuilder method creates a Scene, adds a GridPane, and fills the GridPane with nodes (ImageView pictures, but this should be arbritary).

This is my code for my "UIBuilder" constructor, this 100% works at creating the stage, showing the nodes, etc.

I am trying to add a method to the UIBuilder class, that I can launch from my start class. All I want to do is change a specific node of the static GridPane rootGrid. E.g if rootGrid is an 8x8 grid of images, e.g chess, and I want to the node in 7,7 to a button in lieu of an image. I know that using static nodes is perhaps not best practice, but I am trying to get a hang of the basic gui creation before worrying 100% about the little details. Considering the nodes are static, I'm especially confused. Any general advice about JavaFX usage across classes would be helpful. I've checked through 8 pages of StackOverflow and haven't encountered this issue. Thanks for your patience.

I have tried returning the static GridPane rootGrid to the main class, and then using GridPane root = UIBuilder.getGrid() root.add(arbitraryButton, 7, 7);

I have also tried simply using a method within the UIBuilder class to simply create a button and add it to the static gridPane rootGrid. For this I tried passing the Scene and GridPane using getters from the UIBuilder, thinking that maybe I could pass them back into the UIBuilder to then add a button.

public static void buttonTest(Scene testScene, GridPane passGrid) {

    Button testButton = new Button("Test");
    passGrid.add(testButton, 8, 8);

}



    Class variables
public static Stage cosmos;
public static GridPane rootGrid;
public static Scene rootScene;
public static String testString;
public static Button returnButton;


    UIBuilder(Stage mainStage) {
    GridPane rootGrid = new GridPane();
    Scene scene = new Scene(rootGrid,1000,1600);
    rootScene = scene;

    mainStage.setScene(scene);
    mainStage.show();
    stageTestSize(mainStage, rootGrid);
    rootGrid.setVgap(15);
    rootGrid.setHgap(15);
    
    randomUnits(rootGrid);
}

Under my start method, the constructor below references above code and works perfectly.

UIBuilder uiMaster = new UIBuilder(primaryStage);
        Scene exampleScene = UIBuilder.getScene();
        GridPane exampleGrid = uiMaster.getGrid();
1

There are 1 answers

0
AdamOutler On

Your rootGrid is now your main panel. Gridpanes take Node objects. You can do

  Label myNode=new Label();
  myNode.setGraphic(someOtherNodeOrPictureOrSomething);
  int row=0;
  int column=0;
  rootGrid.add(myNode, column, row);

It's best to set all this up before you .show() because once it is shown, you must use Platform.runlater(()->{...}); to make changes to components. Before the stage is shown, you can create and alter the nodes without lagging the UI.