×

JavaFX Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create a simple button as well as the button with an image. Button in javafx is used to control how the application will work on a particular event occurrence. Also, it is possible to add an event handler to the button and provide action for the same.

There are 2 ways by which we can create a button in JavaFX,

  1. Creating constructor and passing text to it which is to be displayed on the button.
  2. Also, we can use the setText() method with string passed in it.

JavaFX Simple Button:

Example:

import javafx.application.Application;  
import javafx.scene.Scene;
import javafx.scene.control.Button; 
import javafx.scene.layout.StackPane;  
import javafx.stage.Stage;  
  
public class ButtonUI extends Application 
{  
  
    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
          
         StackPane pane = new StackPane();   
         Button newbtn=new Button(" Submit ");  
         Scene scene=new Scene(pane,400,400);  
         pane.getChildren().add(newbtn);  
         primaryStage.setScene(scene);  
         primaryStage.setTitle("JavaFX Button Example");  
         primaryStage.show();  
           
          
    }  
    public static void main ( String[] args ) {  
        launch( args );  
    }  
}

Output:

In order to create a Button in JavaFX, we have to import all the required libraries such as the javafx.application.Application, javafx.scene.Scene,  javafx.scene.control.Button, javafx.scene.layout.StackPane and javafx.stage.Stage. Then we have created one class named ButtonUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold the button, a StackPane object is created which is then passed to the Scene class object. Button object is created and added in the pane. Then the stage is prepared and the title is set to it. The show() method is called to display output. In the main method launch(args) is called to run the application. In output one frame-like container is displayed with a title, JavaFX Button Example and Submit is displayed on the container.

JavaFX Button

JavaFX Button with Image:

The Button class consists of a constructor which is used to accept graphics and text on the button. Below code implements button with image:

Example:

import java.io.FileInputStream;


import javafx.application.Application;  
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;  
import javafx.stage.Stage;  
  
public class ButtonUI extends Application {  
  
    @Override  
    public void start(Stage primaryStage) throws Exception {  
          
    FileInputStream file=new FileInputStream("home.jpg");  
         Image newimage = new Image(file);  
         ImageView imgview=new ImageView(newimage);  
           
         StackPane pane = new StackPane();   
         Button newbtn=new Button(" Home ",imgview);  
         newbtn.setWrapText(true);  
         Scene scene=new Scene(pane,400,400);  
         pane.getChildren().add(newbtn);  
         primaryStage.setScene(scene);  
         primaryStage.setTitle(" JavaFX Button with Image Example");  
         primaryStage.show();
          
    }  
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Button with the image in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene,  javafx.scene.control.Button, javafx.scene.layout.StackPane, javafx.scene.image.Image, javafx.scene.image.ImageView, and javafx.stage.Stage. Then we have created one class named ButtonUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a button with the image, a StackPane object is created which is then passed to the Scene class object. FileInputStream object is created which is then passed to the image constructor. And the image object created with the constructor is passed to the ImageView. Then the Button class object is created, the setWrapText method is called with parameter true to ensure the text is wrapped properly then this button object is passed to StackPane object using the add method, then the Stage is prepared, the title is set to it, and show method is called to display the output. Finally, in the main method, the launch(args) method is called to launch the application. In the output frame like container is displayed with the title JavaFX Button with Image Example and button with a home icon and text home is displayed in the frame.

JavaFX Button

setGraphic method in Button:

The setGraphic method is also provided by the button class in JavaFX to set an image on the button.

import java.io.FileInputStream;


import javafx.application.Application;  
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;  
import javafx.stage.Stage;  
  
public class ButtonUI extends Application {  
  
    @Override  
    public void start(Stage primaryStage) throws Exception {  
          
    FileInputStream file=new FileInputStream("home.jpg");  
         Image newimage = new Image(file);  
         ImageView imgview=new ImageView(newimage);  
           
         StackPane pane = new StackPane();   
         Button newbtn=new Button();  
         newbtn.setGraphic(imgview);
         newbtn.setWrapText(true);  
         Scene scene=new Scene(pane,400,400);  
         pane.getChildren().add(newbtn);  
         primaryStage.setScene(scene);  
         primaryStage.setTitle(" JavaFX Image Button Example");  
         primaryStage.show();
          
    }  
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Button with the image in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene,  javafx.scene.control.Button, javafx.scene.layout.StackPane, javafx.scene.image.Image, javafx.scene.image.ImageView, and javafx.stage.Stage. Then we have created one class named ButtonUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a button with the image, a StackPane object is created which is then passed to the Scene class object. FileInputStream object is created which is then passed to the image constructor. And the image object created with the constructor is passed to the ImageView. Then the Button class constructor is called, then setGraphic() and setWrapText() method is called with parameter ImageView and true respectively. Then this button object is passed to the StackPane object using the add method, then the Stage is prepared, the title is set to it, and the show method is called to display the output. Finally, in the main method, the launch(args) method is called to launch the application. In the output frame like container is displayed with the title JavaFX Image Button Example and a button with the home icon is displayed in the frame.

JavaFX Button

Related Topics

JavaFX Slider

In the JavaFX application, in order to slide over a range of data having values, Slider is used. It provides a pane with a Slider to move over a range...

4 minutes read.

JavaFX Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX TilePane

In the JavaFX application, in order to set TilePane as a layout, the TilePane class is used. The TilePane layout allows us to arrange the components in the same sized...

4 minutes read.

JavaFX Event Filters

In JavaFX, in order to handle the event which are created by any of the mouse action, keyboard action, rotate action, scroll action Event filters are used. The event filters...

3 minutes read.

JavaFX StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack...

4 minutes read.

JavaFX Translate Transition

In the JavaFX application, in order to generate the translate transition, we have to apply translate transition on any particular shape. It has the ability to translate the shape along...

3 minutes read.

JavaFX Architecture

JavaFX architecture consists of many built-in components such as JavaFX public API, Scene Graph, Quantum Toolkit, Prism, Glass windowing toolkit, web view, media engines. These components help to develop rich...

3 minutes read.

JavaFX Rectangle

In the JavaFX application, in order to draw a rectangle, the Rectangle class is used. The Rectangle allows us to join any four points with x and y coordinates in...

4 minutes read.

JavaFX Sphere

In the JavaFX application, in order to draw a Sphere, the Sphere class is used. The Sphere allows us to form a 3D shape having circular faces. All methods needed...

4 minutes read.

JavaFX Scale Transition

In the JavaFX application, in order to generate the scale transition, we have to apply scale transition on any particular shape. It has the ability to scale the shape along...

4 minutes read.

JavaFX Inner Shadow

In the JavaFX application, in order to apply various Inner Shadow effect, we use this class. It has the ability to create the inner shadow of the given object. This...

4 minutes read.

JavaFX Light Point

In the JavaFX application, in order to apply various LightPoint effect from a single source, we use this class. It has the ability to apply light from a single source...

4 minutes read.

JavaFX Pause Transition

In the JavaFX application, in order to generate the pause transition, we have to apply pause transition on any particular shape. It has the ability to take a particular pause...

4 minutes read.

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.

JavaFX Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX UI Controls

We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application...

3 minutes read.

JavaFX TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.

JavaFX Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 minutes read.

JavaFX Path Transition

In the JavaFX application, in order to generate the path transition, we have to apply path transition on any particular shape. It has the ability to traverse the given shape...

3 minutes read.