Change Label text in Button click event : Label « JavaFX « Java






Change Label text in Button click event

 
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {

    Button btn = new Button();
    final Label lbl = new Label();

    primaryStage.setTitle("Hello World!");

    lbl.setLayoutX(70);
    lbl.setLayoutY(150);

    btn.setLayoutX(100);
    btn.setLayoutY(100);
    btn.setText("Hello, World!");

    btn.setOnAction(new EventHandler<ActionEvent>() {

      @Override
      public void handle(ActionEvent event) {
        lbl.setText("Hello, World.");
      }
    });

    Group root = new Group();

    root.getChildren().add(btn);
    root.getChildren().add(lbl);
    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();
  }
}

   
  








Related examples in the same category

1.Using Label to display Text
2.Set new value to Label
3.Set Font for Label
4.Set Label Text color
5.Using Rotate to create vertical label
6.Move a Label by using setTranslateY
7.Wrap a Label
8.Scale a Label
9.Label mouse in and out event
10.Adding Image to Label