JavaFX | Button with examples
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 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.
Commonly used methods:
| method | explanantion |
|---|---|
| setCancelButton(boolean v) | Sets the value of the property cancelButton. |
| setDefaultButton(boolean v) | Sets the value of the property defaultButton |
| isDefaultButton() | Gets the value of the property defaultButton. |
| isCancelButton() | Gets the value of the property cancelButton. |
| cancelButtonProperty() | A Cancel Button is the button that receives a keyboard VK_ESC press |
| defaultButtonProperty() | A default Button is the button that receives a keyboard VK_ENTER press |
| createDefaultSkin() | Create a new instance of the default skin for this control. |
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 stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.StackPane;importjavafx.stage.Stage;publicclassbuttonextendsApplication {// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating buttons");// create a buttonButton b =newButton("button");// create a stack paneStackPane r =newStackPane();// add buttonr.getChildren().add(b);// create a sceneScene sc =newScene(r,200,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:
- 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 itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.Label;importjavafx.stage.Stage;publicclassbutton_1extendsApplication {// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating buttons");// create a buttonButton b =newButton("button");// create a stack paneTilePane r =newTilePane();// create a labelLabel l =newLabel("button not selected");// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){l.setText(" button selected ");}};// when button is pressedb.setOnAction(event);// add buttonr.getChildren().add(b);r.getChildren().add(l);// create a sceneScene sc =newScene(r,200,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

- 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 itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.scene.image.*;importjava.io.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.Label;importjavafx.stage.Stage;importjava.net.*;publicclassbutton_2extendsApplication {// launch the applicationpublicvoidstart(Stage s)throwsException{// set title for the stages.setTitle("creating buttons");// create a input streamFileInputStream input =newFileInputStream("f:\\gfg.png");// create a imageImage i =newImage(input);// create a image ViewImageView iw =newImageView(i);// create a buttonButton b =newButton("", iw);// create a stack paneTilePane r =newTilePane();// create a labelLabel l =newLabel("button not selected");// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){l.setText("button selected ");}};// when button is pressedb.setOnAction(event);// add buttonr.getChildren().add(b);r.getChildren().add(l);// create a sceneScene sc =newScene(r,200,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput

- Java Program to create a button with a image and text and add event handler to it
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 itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.scene.image.*;importjava.io.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.Label;importjavafx.stage.Stage;importjava.net.*;publicclassbutton_3extendsApplication {// launch the applicationpublicvoidstart(Stage s)throwsException{// set title for the stages.setTitle("creating buttons");// create a input streamFileInputStream input =newFileInputStream("f:\\gfg.png");// create a imageImage i =newImage(input);// create a image ViewImageView iw =newImageView(i);// create a buttonButton b =newButton("Button", iw);// create a stack paneTilePane r =newTilePane();// create a labelLabel l =newLabel("button not selected");// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){l.setText("button selected ");}};// when button is pressedb.setOnAction(event);// add buttonr.getChildren().add(b);r.getChildren().add(l);// create a sceneScene sc =newScene(r,200,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

- 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 itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.Label;importjavafx.stage.Stage;publicclassbutton_4extendsApplication {// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating buttons");// create a buttonButton b =newButton("cancel button");// set cancel buttonb.setCancelButton(true);// create a buttonButton b1 =newButton("default button");// set default buttonb1.setDefaultButton(true);// create a stack paneTilePane r =newTilePane();// create a labelLabel l =newLabel("button not selected");// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){l.setText(" cancel button selected ");}};EventHandler<ActionEvent> event1 =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){l.setText(" default button selected ");}};// when button is pressedb.setOnAction(event);b1.setOnAction(event1);// add buttonr.getChildren().add(b);r.getChildren().add(b1);r.getChildren().add(l);// create a sceneScene sc =newScene(r,200,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:


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
Recommended Posts:
- JavaFX | Box with examples
- JavaFX | Arc with examples
- JavaFX | Polygon with examples
- JavaFX | ComboBox with examples
- JavaFX | CheckMenuItem with examples
- JavaFX | CubicCurve with examples
- JavaFX | Circle with examples
- JavaFX | QuadCurve with examples
- JavaFX | Alert with examples
- JavaFX | Sphere with examples
- JavaFX | RadioButton with examples
- JavaFx | ColorPicker with examples
- JavaFX | DatePicker with examples
- JavaFX | PointLight with examples
- JavaFX | Ellipse with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



