user4142722
user4142722

Reputation:

How do I output the contents of an array to a JavaFX Gridpane?

I have an object with a for loop which switches the contents of array elements around for a user-specified number of times. I can currently output the updated array to the Eclipse console at the end of each loop, but I now have to display the updated array inside of a GUI window. I already have working code to display the window and a menu bar with items, but I don't know how to get the array to display it's elements in a grid pane at the end of each loop. I'm supposed to populate the GridPane with rectangles (one for each element of the array), and then have the rectangles move to reflect the changes in the array at the end of each loop. My code is currently as follows:

public void start(Stage stagePrimary) throws Exception {
    stagePrimary.setTitle("My Application");
    //Sets the window title.
    Scene scenePrimary = new Scene(new VBox(), 500, 500);
    // Creates the scene.
    // CODE FOR MENU BAR IS HERE        
    KeyFrame KFSim = new KeyFrame(Duration.millis(1000),
    new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent AE1) {
            /* Simulation stuff (i.e. updating the array)
               is supposed to go here. */
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    //Gridpane is supposed to go here.
                }
        });
    }
});

    stagePrimary.setScene(scenePrimary);
    stagePrimary.show();

The class containing my array is in a class called SimInstance:

public class SimInstance{
    //Irrelevant variables.
    int iNumLoops
    //The number of loops which the simulation should make.
    private char[][] cEditableMap;
    //Array will be updated throughout the simulation.
    public void main(){
        for (int iLoopCount =  0; iLoopCount < iNumLoops; iLoopCount++){
            UpdateMap();
            //Updates the cEditableMap with the new positions.
            PrintMap();
            //Prints the updated map to the Eclipse Console.
        }           
    }
}

I don't currently have the GridPane or the Rectangle objects set up.

Upvotes: 0

Views: 7589

Answers (1)

Angelo Alvisi
Angelo Alvisi

Reputation: 479

If I understood your question correctly:

  1. You probably don't want a Rectangle but a rectangle shaped Label.
  2. If your array is a simple array you probably want a TilePane and not a GridPane. You only want a GridPane if you have an array of arrays.

I'll put some example code, but you should really follow some online tutorials before asking here.

TilePane pane = new TilePane();
for (String s : array){
    Label label = new Label(s, new Rectangle(height, width));
    pane.getChildren().add(label);
}

or:

GridPane pane = new GridPane();
for (int x = 0; x < array.length; x++){
    for (int y = 0; y < array[x].length; y++){
        Label label = new Label(array[x][y], new Rectangle(height, width));
        pane.add(label, x, y);
    }
}

Upvotes: 1

Related Questions