Wolfgang
Wolfgang

Reputation: 11

JavaFX setting Label text from an Array

I'm trying to make a multiple-choice quiz with JavaFX. I would have a Label for displaying the question, an array of Strings to hold the questions and an array of ToggleGroups for displaying the possible answers.

If the user presses the "Next" button, the Label should change its text and display the next element from the array of Strings, also the text for possible answers should change accordingly, however, I can't get this to work for even the Label.

The current code I have just iterates through the array and only displays the last element once, but I need it to start from the first question and display the next question each time the user presses "Next". Any help would be much appreciated.

This is just an example of what I'm trying to do:

public class setLay extends Application{

String[]text = new String[4];

Label label = new Label();

Button next = new Button("Next");
@Override

public void start(Stage primaryStage) throws Exception {

    text[0]= "What's the capital of Monaco?";
    text[1] = "What's the capital of San Marino?";
    text[2] = "What's the capital of Lithuania?";
    text[3] = "What's the capital of Denmark?";

    label.setText(text[0]);
    label.setTranslateX(200);

    next.setOnAction(e -> makeLabel());

    Stage stage = new Stage();
    Pane pane = new Pane();
    Scene scene = new Scene(pane, 800, 500);
    stage.setScene(scene);
    stage.show();

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

    }

public void makeLabel() {

    for(int i=0; i<text.length; i++) {

        label.setText(text[i]);

          }

       }



    }

Upvotes: 0

Views: 3534

Answers (1)

James_D
James_D

Reputation: 209408

Just store the current question in an instance variable and increment it on each button click:

public class setLay extends Application{

    private int currentQuestionIndex = 0 ;

    String[]text = new String[4];

    Label label = new Label();

    Button next = new Button("Next");
    @Override

    public void start(Stage primaryStage) throws Exception {

        text[0]= "What's the capital of Monaco?";
        text[1] = "What's the capital of San Marino?";
        text[2] = "What's the capital of Lithuania?";
        text[3] = "What's the capital of Denmark?";

        label.setText(text[0]);
        label.setTranslateX(200);

        next.setOnAction(e -> makeLabel());

        Stage stage = new Stage();
        Pane pane = new Pane();
        Scene scene = new Scene(pane, 800, 500);
        stage.setScene(scene);
        stage.show();

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

    }

    public void makeLabel() {
        currentQuestionIndex = (currentQuestionIndex + 1) % text.length ;
        label.setText(text[currentQuestionIndex]);
    }
}

If you use an IntegerProperty instead of a plain int, you can do all sorts of fun things:

private IntegerProperty currentQuestionIndex = new SimpleIntegerProperty();

// ...

// label.setText(text[0]);
label.textProperty().bind(Bindings.createStringBinding(() -> 
    text[currentQuestionIndex.get()], currentQuestionIndex);

next.disableProperty().bind(currentQuestionIndex.isEqualTo(text.length -1 ));

// next.setOnAction(e -> makeLabel());
next.setOnAction(e -> currentQuestionIndex.set(currentQuestionIndex.get()+1));

Upvotes: 1

Related Questions