Alex Roman
Alex Roman

Reputation: 61

Can't get a Label to update its text using JavaFX

I'm a beginner trying to learn java from the Java MOOC fi course. I'm trying to to a tic tac toe game. I'm trying to add functionality to the buttons so that when I press them the info label at the top changes (showing who's turn it is) but I can't get the label to update. I tried looking up the solution myself but I really couldn't figure it out.

package ticTacToe;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.ArrayList;

public class TicTacToeApplication extends Application {
    ArrayList<Button> buttons = new ArrayList<>();
    String currentPlayer = "X";

    public static void main(String[] args) {
        launch(TicTacToeApplication.class);
    }

    @Override
    public void start(Stage window) throws  Exception{
        BorderPane layout = new BorderPane();
        layout.setPadding(new Insets(20));

        Label info = new Label("Turn: " + currentPlayer);
        info.setFont(Font.font(40));

        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);

        //create 9 buttons with the same functionality
        for (int i = 0; i < 9; i++) {
            Button button = new Button();
            button.setFont(Font.font(20));
            button.setMinSize(40,40);
            button.setMaxSize(40, 40);

            //button functionality
            button.setOnMouseClicked(event -> {
                button.setText(currentPlayer);
                changeTurn();
                info.setText("Turn: " + currentPlayer);
            });
            buttons.add(button);
        }
        //add buttons to the grid
        int buttonNr = 0;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j <3; j++) {
                grid.add(buttons.get(buttonNr),i,j);
                buttonNr++;
            }
        }

        //setup main layout
        layout.setTop(info);
        layout.setCenter(grid);

        //setup and start scene
        Scene scene = new Scene(layout);
        window.setTitle("TicTacToe");
        window.setScene(scene);
        window.show();
    }

    public void changeTurn(){
        if (currentPlayer.equals("X")) {
            currentPlayer = "O";
        }
        if (currentPlayer.equals("O")) {
            currentPlayer = "X";
        }
    }
}

Upvotes: 3

Views: 101

Answers (1)

Alex Roman
Alex Roman

Reputation: 61

As James_D said, the issue was a very basic one in the changeTurn method. I was always changing back to X's turn.

I added return to the first if and now it works.

public void changeTurn(){
    if (currentPlayer.equals("X")) {
        currentPlayer = "O";
        return;
    }
    if (currentPlayer.equals("O")) {
        currentPlayer = "X";
    }

Upvotes: 3

Related Questions