JavaFX | Button with examples

28 Oct, 2019
Button class is a part of JavaFX package and it can have a text or graphic or both.
Button in JavaFX can be of three different types:
Normal Button: A normal push buttonDefault Button: A default button that receives a keyboard VK_ENTER pressCancel Button: A cancel button that receives a keyboard VK_ENTER press
Normal Button: A normal push button
Default Button: A default button that receives a keyboard VK_ENTER press
Cancel Button: A cancel button that receives a keyboard VK_ENTER press
When the button is pressed an Action Event is sent. This Action Event can be managed by an EventHandler. Buttons can also respond to mouse events by implementing an EventHandler to process the MouseEvent.
Constructor of the Button class are:
Button(): creates a button with an empty string for its label.Button(String t): creates a button with the specified text as its label.Button(String t, Node g): creates a button with the specified text and icon for its label.
Button(): creates a button with an empty string for its label.
Button(String t): creates a button with the specified text as its label.
Button(String t, Node g): creates a button with the specified text and icon for its label.
Commonly used methods:
Below programs illustrate the use of Button in JavaFX.
Program to create a button and add it to the stage: This program creates a Button indicated by the name b. The button will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button inside the scene. Finally, the show() method is called to display the final results.// Java Program to create a button and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.StackPane;import javafx.stage.Stage;public class button extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating buttons");          // create a button        Button b = new Button("button");          // create a stack pane        StackPane r = new StackPane();          // add button        r.getChildren().add(b);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:Java program to create a button and add event handler to it: This program creates a Button indicated by the name b. The button will be created inside a scene, which in turn will be hosted inside a stage. We would create a label to show if the button is pressed or not. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function.// Java program to create a button and add event handler to itimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;public class button_1 extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating buttons");          // create a button        Button b = new Button("button");          // create a stack pane        TilePane r = new TilePane();          // create a label        Label l = new Label("button not selected");          // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("   button   selected    ");            }        };          // when button is pressed        b.setOnAction(event);          // add button        r.getChildren().add(b);        r.getChildren().add(l);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:Java Program to create a button with a image and add event handler to it: This program creates a Button with an image on it indicated by the name b. The image will be included using the File Input Stream that imports the image. we will then create an image using the object of file input stream and then create an image view using the image file. The button will be created inside a scene, which in turn will be hosted inside a stage.we would create a label to show if the button is pressed or not. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function.// Java Program to create a button with a image and// add event handler to itimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.scene.image.*;import java.io.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;import java.net.*;public class button_2 extends Application {      // launch the application    public void start(Stage s) throws Exception    {        // set title for the stage        s.setTitle("creating buttons");          // create a input stream        FileInputStream input = new FileInputStream("f:\\gfg.png");          // create a image        Image i = new Image(input);          // create a image View        ImageView iw = new ImageView(i);          // create a button        Button b = new Button("", iw);          // create a stack pane        TilePane r = new TilePane();          // create a label        Label l = new Label("button not selected");          // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("button selected    ");            }        };          // when button is pressed        b.setOnAction(event);          // add button        r.getChildren().add(b);        r.getChildren().add(l);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}OutputJava Program to create a button with a image and text and add event handler to itThis program creates a Button with an image and a text on it indicated by the name b. The image will be included using the File Input Stream that imports the image. we will then create an image using the object of file input stream and then create an image view using the image file.The button will be created inside a scene, which in turn will be hosted inside a stage.we would create a label to show if the button is pressed or not. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function.// Java Program to create a button with a image// and text and add event handler to itimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.scene.image.*;import java.io.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;import java.net.*;public class button_3 extends Application {      // launch the application    public void start(Stage s) throws Exception    {        // set title for the stage        s.setTitle("creating buttons");          // create a input stream        FileInputStream input = new FileInputStream("f:\\gfg.png");          // create a image        Image i = new Image(input);          // create a image View        ImageView iw = new ImageView(i);          // create a button        Button b = new Button("Button", iw);          // create a stack pane        TilePane r = new TilePane();          // create a label        Label l = new Label("button not selected");          // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("button selected    ");            }        };          // when button is pressed        b.setOnAction(event);          // add button        r.getChildren().add(b);        r.getChildren().add(l);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:Java program to create a default button and a cancel button: This program creates a Button indicated by the name b and b1 . The button b will act as a cancel button which will respond to the escape keypress of keyboard and button b1 will behave as a default button which will respond to enter keypress of the keyboard). The button will be created inside a scene, which in turn will be hosted inside a stage.we would create a label to show which button is pressed. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function.// Java program to create a default button and a// cancel button and add event handler to itimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;public class button_4 extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating buttons");          // create a button        Button b = new Button("cancel button");          // set cancel button        b.setCancelButton(true);          // create a button        Button b1 = new Button("default button");          // set default button        b1.setDefaultButton(true);          // create a stack pane        TilePane r = new TilePane();          // create a label        Label l = new Label("button not selected");          // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("  cancel  button    selected    ");            }        };        EventHandler<ActionEvent> event1 = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("  default button   selected    ");            }        };          // when button is pressed        b.setOnAction(event);        b1.setOnAction(event1);          // add button        r.getChildren().add(b);        r.getChildren().add(b1);        r.getChildren().add(l);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:
Program to create a button and add it to the stage: This program creates a Button indicated by the name b. The button will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button inside the scene. Finally, the show() method is called to display the final results.// Java Program to create a button and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.StackPane;import javafx.stage.Stage;public class button extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating buttons");          // create a button        Button b = new Button("button");          // create a stack pane        StackPane r = new StackPane();          // add button        r.getChildren().add(b);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:
// Java Program to create a button and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.StackPane;import javafx.stage.Stage;public class button extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating buttons");          // create a button        Button b = new Button("button");          // create a stack pane        StackPane r = new StackPane();          // add button        r.getChildren().add(b);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}
Output:
Java program to create a button and add event handler to it: This program creates a Button indicated by the name b. The button will be created inside a scene, which in turn will be hosted inside a stage. We would create a label to show if the button is pressed or not. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function.// Java program to create a button and add event handler to itimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;public class button_1 extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating buttons");          // create a button        Button b = new Button("button");          // create a stack pane        TilePane r = new TilePane();          // create a label        Label l = new Label("button not selected");          // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("   button   selected    ");            }        };          // when button is pressed        b.setOnAction(event);          // add button        r.getChildren().add(b);        r.getChildren().add(l);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:
// Java program to create a button and add event handler to itimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;public class button_1 extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating buttons");          // create a button        Button b = new Button("button");          // create a stack pane        TilePane r = new TilePane();          // create a label        Label l = new Label("button not selected");          // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("   button   selected    ");            }        };          // when button is pressed        b.setOnAction(event);          // add button        r.getChildren().add(b);        r.getChildren().add(l);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}
Output:
Java Program to create a button with a image and add event handler to it: This program creates a Button with an image on it indicated by the name b. The image will be included using the File Input Stream that imports the image. we will then create an image using the object of file input stream and then create an image view using the image file. The button will be created inside a scene, which in turn will be hosted inside a stage.we would create a label to show if the button is pressed or not. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function.// Java Program to create a button with a image and// add event handler to itimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.scene.image.*;import java.io.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;import java.net.*;public class button_2 extends Application {      // launch the application    public void start(Stage s) throws Exception    {        // set title for the stage        s.setTitle("creating buttons");          // create a input stream        FileInputStream input = new FileInputStream("f:\\gfg.png");          // create a image        Image i = new Image(input);          // create a image View        ImageView iw = new ImageView(i);          // create a button        Button b = new Button("", iw);          // create a stack pane        TilePane r = new TilePane();          // create a label        Label l = new Label("button not selected");          // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("button selected    ");            }        };          // when button is pressed        b.setOnAction(event);          // add button        r.getChildren().add(b);        r.getChildren().add(l);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output
// Java Program to create a button with a image and// add event handler to itimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.scene.image.*;import java.io.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;import java.net.*;public class button_2 extends Application {      // launch the application    public void start(Stage s) throws Exception    {        // set title for the stage        s.setTitle("creating buttons");          // create a input stream        FileInputStream input = new FileInputStream("f:\\gfg.png");          // create a image        Image i = new Image(input);          // create a image View        ImageView iw = new ImageView(i);          // create a button        Button b = new Button("", iw);          // create a stack pane        TilePane r = new TilePane();          // create a label        Label l = new Label("button not selected");          // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("button selected    ");            }        };          // when button is pressed        b.setOnAction(event);          // add button        r.getChildren().add(b);        r.getChildren().add(l);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}
Output
Java Program to create a button with a image and text and add event handler to itThis program creates a Button with an image and a text on it indicated by the name b. The image will be included using the File Input Stream that imports the image. we will then create an image using the object of file input stream and then create an image view using the image file.The button will be created inside a scene, which in turn will be hosted inside a stage.we would create a label to show if the button is pressed or not. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function.// Java Program to create a button with a image// and text and add event handler to itimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.scene.image.*;import java.io.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;import java.net.*;public class button_3 extends Application {      // launch the application    public void start(Stage s) throws Exception    {        // set title for the stage        s.setTitle("creating buttons");          // create a input stream        FileInputStream input = new FileInputStream("f:\\gfg.png");          // create a image        Image i = new Image(input);          // create a image View        ImageView iw = new ImageView(i);          // create a button        Button b = new Button("Button", iw);          // create a stack pane        TilePane r = new TilePane();          // create a label        Label l = new Label("button not selected");          // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("button selected    ");            }        };          // when button is pressed        b.setOnAction(event);          // add button        r.getChildren().add(b);        r.getChildren().add(l);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:
This program creates a Button with an image and a text on it indicated by the name b. The image will be included using the File Input Stream that imports the image. we will then create an image using the object of file input stream and then create an image view using the image file.The button will be created inside a scene, which in turn will be hosted inside a stage.we would create a label to show if the button is pressed or not. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function.
// Java Program to create a button with a image// and text and add event handler to itimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.scene.image.*;import java.io.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;import java.net.*;public class button_3 extends Application {      // launch the application    public void start(Stage s) throws Exception    {        // set title for the stage        s.setTitle("creating buttons");          // create a input stream        FileInputStream input = new FileInputStream("f:\\gfg.png");          // create a image        Image i = new Image(input);          // create a image View        ImageView iw = new ImageView(i);          // create a button        Button b = new Button("Button", iw);          // create a stack pane        TilePane r = new TilePane();          // create a label        Label l = new Label("button not selected");          // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("button selected    ");            }        };          // when button is pressed        b.setOnAction(event);          // add button        r.getChildren().add(b);        r.getChildren().add(l);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}
Output:
Java program to create a default button and a cancel button: This program creates a Button indicated by the name b and b1 . The button b will act as a cancel button which will respond to the escape keypress of keyboard and button b1 will behave as a default button which will respond to enter keypress of the keyboard). The button will be created inside a scene, which in turn will be hosted inside a stage.we would create a label to show which button is pressed. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function.// Java program to create a default button and a// cancel button and add event handler to itimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;public class button_4 extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating buttons");          // create a button        Button b = new Button("cancel button");          // set cancel button        b.setCancelButton(true);          // create a button        Button b1 = new Button("default button");          // set default button        b1.setDefaultButton(true);          // create a stack pane        TilePane r = new TilePane();          // create a label        Label l = new Label("button not selected");          // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("  cancel  button    selected    ");            }        };        EventHandler<ActionEvent> event1 = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("  default button   selected    ");            }        };          // when button is pressed        b.setOnAction(event);        b1.setOnAction(event1);          // add button        r.getChildren().add(b);        r.getChildren().add(b1);        r.getChildren().add(l);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:
// Java program to create a default button and a// cancel button and add event handler to itimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;public class button_4 extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating buttons");          // create a button        Button b = new Button("cancel button");          // set cancel button        b.setCancelButton(true);          // create a button        Button b1 = new Button("default button");          // set default button        b1.setDefaultButton(true);          // create a stack pane        TilePane r = new TilePane();          // create a label        Label l = new Label("button not selected");          // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("  cancel  button    selected    ");            }        };        EventHandler<ActionEvent> event1 = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("  default button   selected    ");            }        };          // when button is pressed        b.setOnAction(event);        b1.setOnAction(event1);          // add button        r.getChildren().add(b);        r.getChildren().add(b1);        r.getChildren().add(l);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}
Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Button.html
shubham_singh
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.