1

We have this warm up exercise where we're supposed to create this reallllly simple game which's UI is pretty much set up

which's UI is pretty much set up.

I got the error "Local variable i defined in an enclosing scope must be final or effectively final".

I didn't understand it, so I googled it, but most of the problems are different. While typing this question I found this in the stackoverflow suggestions:

Assigning an action to each button, in an array of buttons in JavaFX

But I simply don't understand. I'm learning programming/java from scratch. I hope JavaFX/GuI stuff isn't a hindrance?

The code below only includes my attempt to assign the actions. I separated it from the creation of the buttons for the time being to figure out what the problem is. The problem is only in the if and else conditions.

    for(int i=0; i<=4; i++) {

        for(int j=0; j<=4; j++) {

            buttonGrid[i][j].setOnAction(new EventHandler<ActionEvent>() {
                public void handle(ActionEvent event) {
                    
                    if (buttonGrid[i][j].getText() == "X") {
                        buttonGrid[i][j].setText("O");
                    } else {
                        buttonGrid[i][j].setText("X");
                    }
                }
            });
         }
    }

For now I just want the button's labels to change from X to O and from O to X as you click them. Btw. If I learn JavaFX and GUI, does it mean I HAVE to learn css? (Not that I don't want to, just.. not now)

If there is a need for the rest of the code to figure the problem: http://textuploader.com/5b1kh

I'd also appreciate if someone could tell me how to do the Scenes in a more efficent way. (Btw, can I somehow lock the aspect ratio of the sides of all cells of a gridpane?)

Community
  • 1
  • 1
Duc Nguyen
  • 165
  • 1
  • 5
  • The error is because variables are copied inside an anonymous class and so has to be final. Better explanation here :http://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class – Vivin May 10 '16 at 04:56

1 Answers1

1

I think that the answer in this question explains your problem very well and how to solve it Problems with local variable scope. How to solve it? You can not use i and j inside the Action handler.

Try this. [Notice that I've also changed the string comparison*]

 for(int i=0; i<=4; i++) {

    for(int j=0; j<=4; j++) {

        final Button myButton = buttonGrid[i][j];

        myButton.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                if ("X".equals(myButton.getText())) {
                    myButton.setText("O");
                } else {
                    myButton.setText("X");
                }
            }
        });
    }
} 

[*] How do I compare strings in Java?

Community
  • 1
  • 1
RubioRic
  • 2,442
  • 4
  • 28
  • 35
  • Thank you very much! I got it to work, but I used Button, idk what JButton is and I kept my String comparison, because yours for some reason gave me a Syntax error, although there should be none.. edit: nvm there was just a bracket missing – Duc Nguyen May 10 '16 at 17:09
  • @HenryLa You're welcome. JButton is a Swing class. More or less the previous version of JavaFx. My mistake. Mark the answer as accepted if it has been useful, please. – RubioRic May 10 '16 at 17:23