Vasque
Vasque

Reputation: 3

How to display a label with a variable value? (JavaFX)

I am using NetBeans and JavaFX to create a minigame that have a Attack Button and a Enemy - just like Pokemon...

But I am Stuck trying to set a variable value (Variable 'hp' - value - 100) on a label to everytime I press the Attack button, the value of var 'hp' decreases by my damage value and the label show it's current 'hp'.

TL;DR - I need to display a variable value on a label on JavaFX

Thank you very much!

Upvotes: 0

Views: 6279

Answers (1)

Cobusve
Cobusve

Reputation: 1570

Looks like you are using SceneBuilder.

Your controller should have something like this at the top somewhere

@FXML
Label myLabel;

In order to change the text on the label you simply do this in the onAction for the button.

  @FXML
  void onAction(ActionEvent event) {
       myLabel.setText(hp.toString());
  }

Upvotes: 2

Related Questions