JavaFX Tutorial - Java Button(java.lang.String text) Constructor








Syntax

Button(java.lang.String text) constructor from Button has the following syntax.

public Button(java.lang.String text)

Example

In the following code shows how to use Button.Button(java.lang.String text) constructor.

//  w ww . j  a v a 2 s . c om
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.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250);
        Button btn = new Button("Hello World");
        btn.setLayoutX(100);
        btn.setLayoutY(80);
        btn.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                System.out.println("Hello World");
            }
        });
        root.getChildren().add(btn);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}