chujudzvin
chujudzvin

Reputation: 1333

JavaFX. How to change value of a label. Error: variable needs to be final or effectively final

I have a simple window with a button correctBut and a label Label pointsLbl = new Label("0") . I want the text of the pointsLbl to change everytime i hit the button. Initially the text of pointsLbl is "0". Then when I hit the button it should be "1";

So I created an additional variable int points, which is also initially 0. I thought I could add +1 to the value of points in the EventHandler, and than convert it to string and set it as a new text.

String newValStr = points.toString();
pointsLbl.setText(newValStr);

But I get the following Error: "variable points is accessed from within an inner class, needs to be final or effectively final".

So, how should have written the code, so that I could change values and then setText to the pointsLbl?

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class QuickGameWindow {

    public static void display() {

        Stage window = new Stage();

        int points = 0;
        String parsedPoints = "";

        window.setTitle("New Window");

        GridPane grid = new GridPane();
        grid.setPadding(new Insets(10,10,10,10));
        grid.setVgap(20);
        grid.setHgap(10);

        Button correctBut = new Button("Correct");
        Label textLbl = new Label("Points - ");
        Label pointsLbl = new Label("0");

        correctBut.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {

            //Here comes the problem. I cannot change the value of points.

                points++;
                String newValStr = points.toString();
                pointsLbl.setText(newValStr);

            }
        });


        GridPane.setConstraints(correctBut, 3, 3);
        GridPane.setConstraints(pointsLbl, 1, 5);
        GridPane.setConstraints(textLbl, 3, 5);

        grid.getChildren().addAll(correctBut,pointsLbl,textLbl);
        Scene scene = new Scene(grid, 300, 200);

        window.setScene(scene);
        window.show();

    }
}

Upvotes: 0

Views: 1414

Answers (1)

hotzst
hotzst

Reputation: 7526

As you are you using a lambda expression for your event handler all variable defined in the outer scope must be either final or member variables. This leaves you with tow options:

1) Make the counter points a member variable:

private int points = 0;

2) Use a locale IntegerProperty instead of an int:

IntegerProperty points = new SimpleIntegerProperty(0);
...
correctBut.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent e) {
        points.set(points.get() + 1);
        String newValStr = points.toString();
        pointsLbl.setText(newValStr);
    }
});

Upvotes: 2

Related Questions