×

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can also be changed.

JavaFX Button with specified height and width:

Example:

import java.io.FileInputStream;
import javafx.application.Application;  
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;  
import javafx.stage.Stage;  
import javafx.event.ActionEvent;  
import javafx.event.EventHandler;  
  
public class ButtonUI extends Application  
{  
  
    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
          
   
          StackPane pane = new StackPane();   
          Button newbtn=new Button(" Submit ");  
          newbtn.setMaxWidth(200);
          newbtn.setMaxHeight(100);
          newbtn.setOnAction(new EventHandler<ActionEvent>() 
          {  
                
              @Override  
              public void handle(ActionEvent arg0) {  
                  System.out.println(" Button with specified size is clicked");  
                    
              }  
          } );  
            
          Scene scene=new Scene(pane,400,400);  
          pane.getChildren().add(newbtn);  
          primaryStage.setScene(scene);  
          


        primaryStage.setTitle(" JavaFX Button with specified max width and height ");  
          primaryStage.show();  
            
      }  
      public static void main(String[] args) {  
          launch(args);  
      }  


  }

Output:

In order to create the Button with specified width and height in JavaFX and add action on it, 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, javafx.stage.Stage, javafx.event.ActionEvent and javafx.event.EventHandler. 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. Then the Button class constructor is created with the name of the button passed to it, then the setMaxWidth() and setMaxHeight() method is called to specify the maximum width and height of the button. Then setOnAction() method is called on an instance of Button class and overridden handle method with the anonymous class object passed to it. Then button object is added to the StackPane container, the stage is prepared, the title is set, and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX Button with specified max width and height”. Also, Submit button is displayed with a width 200 and a height 100.

JavaFX Button Styling

JavaFX Button with specified CSS Styling:

We can apply CSS styles such as border, text color on the button in JavaFX.

Example:

import javafx.application.Application;  
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;  
import javafx.stage.Stage;  
  
public class ButtonUI extends Application   
{  
  
    @Override  


    public void start(Stage primaryStage) throws Exception 
    {  
          


        Button newbn = new Button(" File ");
        Button newbn2 = new Button(" Edit ");
        Button newbn3 = new Button(" Help ");
     


        newbn.setStyle("-fx-border-color: #ff0000; -fx-border-width: 2px;");
        newbn2.setStyle("-fx-background-color: #89cff0");
        newbn3.setStyle("-fx-text-fill: #009000");




        HBox hbox = new HBox(newbn, newbn2, newbn3);




        Scene scene = new Scene(hbox, 400, 400);
        primaryStage.setTitle(" JavaFX button with CSS styling ");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) 
  {
        Application.launch(args);
    }


}

Output:

In order to create the Buttons with specified CSS styling 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, javafx.stage.Stage, javafx.scene.layout.HBox. 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. Then 3 buttons are created using the constructor and name specified to them. Style is set to each button using setStyle() method. Various CSS styling such as border colour and width, background colour, text colour is added in the setStyle() method. Then HBox object is created with 3 buttons passed to it. Scene class object is created with HBox object passed to it. Then the title is set and the show() method is called to display output. In order to run application, Application.launch(args) method is called in main() method. In the output, Frame like container is displayed with 3 buttons. The first button is File. It is created with border color red and border width 2 px specified. The second button, Edit has a background color sky blue. The third button Help is created with the text color green.

JavaFX Button Styling

Related Topics

JavaFX Image Input

In the JavaFX application, in order to apply various image input effect, we use the ImageInput class. It has the ability to fill the specified shape with a given image...

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 Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

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 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 PasswordField

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

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 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 Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 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 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 Box

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

4 minutes read.

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX Drop Shadow

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

4 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 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 Reflection

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

4 minutes read.

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.