Creates a JavaFX button with the given label and action handler. - Java JavaFX

Java examples for JavaFX:Button

Description

Creates a JavaFX button with the given label and action handler.

Demo Code


import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;

public class Main{
    /**/*w  ww . j a v a 2 s  .  c  om*/
     * Creates a button with the given label and action handler.
     *
     * @param label The string to display in the button.
     *
     * @param handler The handler to be invoked when the user presses the button.
     *
     * @return The button that is created.
     */
    public static Button createButton(String label,
            EventHandler<ActionEvent> handler) {
        Button button = new Button(label);
        if (handler != null) {
            button.setOnAction(handler);
        }
        button.setMaxWidth(Double.MAX_VALUE);
        button.setMinWidth(Button.USE_PREF_SIZE);
        button.defaultButtonProperty().bind(button.focusedProperty());
        return button;
    }
    /**
     * Creates a button with the given label.
     *
     * @param label The string to display in the button.
     *
     * @return The button that is created.
     */
    public static Button createButton(String label) {
        return createButton(label, null);
    }
}

Related Tutorials