JavaFX: Which Button Is Clicked

This entry is part 24 of 55 in the series JavaFX

To test which button is clicked, I have made use of setOnAction() method of the button object and called the relevant function accordingly.

package javafxapplication20;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class JavaFXApplication20 extends Application {

    Button movButton = new Button("Movie");

    Button gameButton = new Button("Game");

    Label lb = new Label("Which Do You Prefer?");

    public void start(Stage stage) {

        BorderPane border = new BorderPane();

        border.setPrefSize(500, 400);

        movButton.setOnAction(e -> movieButton());
        gameButton.setOnAction(e -> gameButton());

        HBox hbox = addHorizontalBox();

        Scene scene = new Scene(border);
        stage.setTitle("Which Button");;

    private void movieButton() {

        lb.setText("Movie Button Is Clicked");

    private void gameButton() {

        lb.setText("Game is Chosen");


    private HBox addHorizontalBox() {

        HBox hbox = new HBox();
        hbox.setPadding(new Insets(15, 12, 15, 12));
        hbox.setSpacing(10);   // Gap between nodes

        hbox.getChildren().addAll(movButton, gameButton);

        return hbox;

    private VBox addVBox() {

        VBox vbox = new VBox();
        vbox.setPadding(new Insets(50)); // Set all sides to 10

        lb.setFont(Font.font("Consolas", FontWeight.BOLD, 24));

        return vbox;

    public static void main(String[] args) {



Series Navigation<< JavaFX: Removing CSS effects to a Button
JavaFX: Using a Click Event Handler >>