Home > OS >  JavaFX: Update a label with new text every time one button is clicked
JavaFX: Update a label with new text every time one button is clicked

Time:11-23

I'm trying to make an interactive visual novel for my uni project, but I can't figure out how to update the label's text to the next line of dialogue with only one button.

public void NicoleDialogue() {
    String[] NDialogue = {"My name is Tala Nicole Dimaapi Valdez / Joshua Manuel Garcia Reyes, I know a long ass name but I’m filipino what can I say?",
    "I’m a student of DLSU, one of the most prestigious universities in the country and today I will join GreenGiant FM, an organization for people who want to become radio hosts or who are just interested in radio hosting in general.",
    "It is my first time joining an organization in this school so I’m a bit nervous since I don’t really know anybody from the org but there is no time for getting nervous right now since I’m about to open the door"};

    for(int i = 0; i < NDialogue.length; i  ) {
        NLine1.setText(NDialogue[i]);
    }
}

So far I've tried making an array of dialogue I want to display and a loop that loops through the dialogue with every click but it doesn't work and I'm pretty much lost at this point. Any help is apreciated. Thank you!

CodePudding user response:

a loop that loops through the dialogue with every click

You have to change your thinking. You don’t use a loop for this.

GUIs are event driven. Users and systems generate events that you need to handle:

  1. Store an index.
  2. Provide an event handler.
    • see the button setOnAction javadoc.
  3. In the event handler, after checking that text remains:
    • increment the index,
    • set the text of your label to the newly indexed line.
  • Related