JavaFX Button

0

Javafx button tutorial:

button is used to perform some task on clicking.

Constructor for button :

  • Button()
    A button with an empty string for its label.
  • Button(String text)
    A button with string for its label.
  • Button(String text, Node graphic)
    A button with the string and icon for its label.

Example of javafx button:


package javafxtuts;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;


/**
 *
 * @author javafxtuts
 */
public class Javafxtuts extends Application {
    
    

  @Override
  public void start(Stage primaryStage) {
      primaryStage.setTitle("javafxtuts.com");
      double width = 400;
      double height = 300;
      GridPane root = new GridPane();
      Button button = new Button();
      button.setText("javafxtuts.com");
//for set padding for root(Gridpane)
      root.setPadding(new Insets(70));
      root.getChildren().add(button);
      Scene scene = new Scene(root, width, height);
      primaryStage.setScene(scene);
      primaryStage.show();
  }
  

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

Output:

Explanation of button example :

    • To use button in our application we create button AS

Button button = new Button();

    • setText(“javafxtuts.com”) method set text or label of button to javafxtuts.com
    • Set space or padding using setPadding() method

root.setPadding(70);

  • Which set the space of 70 for root(Gridpane Layout)
    in all directions.
Share.

Leave A Reply