Ondra Jahoda
Ondra Jahoda

Reputation: 23

JavaFX change label after button click

im trying to show one line of matrix each time. But when button is pressed i want to show next line of that matrix. My idea was that i show line with index "index" and create action on button press that add 1 to variable "index". It doesnt semms to be good idea, because its not working. Its showing only the first line, and never changes.

public class GUI extends Application {
int index = 0;
public static int save[][] = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}};


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

@Override
public void start(Stage primaryStage) {
    Label label = new Label(Arrays.toString(save[index]));


    Button next = new Button();
    next.setText("Next");
    next.setOnAction(e -> {
        dalsi();
    });

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

    GridPane.setConstraints(label, 5, 6);
    GridPane.setConstraints(next, 6, 13);

    grid.getChildren().addAll(label, next);

    Scene scene = new Scene(grid, 250, 180);
    primaryStage.setScene(scene);
    primaryStage.setTitle("QuickSort");
    primaryStage.show();
}

public void dalsi() {
    if (index < Quicksort.delka - 1) {
        index++;
    }
}
}

Upvotes: 0

Views: 3939

Answers (1)

jewelsea
jewelsea

Reputation: 159566

To make label text change, you need to call setText on the label when you want the text to change.

Make your label a member variable for the class, then write:

label.setText(
    Arrays.toString(save[index])
);

after you call index++ in your dalsi() method.

Upvotes: 1

Related Questions