Table of contents
1.
Introduction
2.
Inline CSS in JavaFX
2.1.
Syntax:
3.
Example Application
3.1.
Example 1:
3.1.1.
JavaFXcss.java
3.2.
Example 2:
3.2.1.
Css.java
3.2.2.
Explanation:
4.
Frequently Asked Questions
4.1.
Is CSS supported by JavaFX?
4.2.
Where do I put CSS in JavaFX?
4.3.
What is inset JavaFX?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX Inline Styles

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will learn about the JavaFX Inline Styles. JavaFX is the next-generation client application platform for Java-based desktop, mobile, and embedded computers. It results from a cooperative effort between numerous people and businesses to create a cutting-edge, practical, and feature-rich toolkit for creating rich client applications. The multiple CSS classes offered by JavaFX will be used in the JavaFX application to apply various Inline CSS effects. It involves different CSS effects to the given object to improve the user's understanding of the interface. This article describes the JavaFX Inline Styles, beginning with the most straightforward statements and progressing to the most significant modules.
 

Inline CSS in JavaFX

JavaFX allows you to use CSS to improve the look and feel of your application. The classes used to apply CSS to JavaFX applications are in the package javafx.css. To apply numerous Inline CSS effects in the JavaFX application, we will leverage the many CSS classes available in JavaFX. It is used to add numerous CSS effects to a given object to clarify the user interface. We utilise Inline style CSS to give varied colours, backdrops, text sizes, font style, spacings, paragraphs, and alignments for a specific item when designing webpages. In JavaFX, we use inline styles to define CSS rules within the JavaFX application. Generally, rules specified in JavaFX application code take precedence over rules written in a separate CSS file. In JavaFX code, we may apply the CSS rule to a specific node as follows:

Syntax:

You may use the setStyle() method to add inline styles. These styles are solely made up of key-value pairs and only apply to the nodes where they are set. The code for adding an inline style sheet to a button is shown below.

Code:

Label label_name= new Label(“label_id”);
label_name.setStyle(/*CSS Properties*/);  

 

Example Code:

Label CodingNinjas= new Label(“Percentage”);
CodingNinjas.setStyle("-fx-background-color:Orange; -fx-text-fill:Blue; -fx-font-size:14");  

Example Application

Example 1:

Assume we've created a JavaFX application that displays a form with a Text Field, a Password Field, and two buttons. By default, this form appears, as shown in the screenshot below.


 

The following programme is an example of how to apply styles to the preceding application in JavaFX.

JavaFXcss.java

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.scene.control.TextField;
import javafx.stage.Stage;  

public class JavaFXcss extends Application {
   @Override
   public void start(Stage stage) {      
      //creating label email
      Text text1 = new Text("Email");      
     
      //creating label password
      Text text2 = new Text("Password");
       
      //Creating Text Filed for email        
      TextField textField1 = new TextField();      
     
      //Creating Text Filed for password        
      PasswordField textField2 = new PasswordField();  
       
      //Creating Buttons
      Button button1 = new Button("Submit");
      Button button2 = new Button("Clear");  
     
      //Creating a Grid Pane
      GridPane cnGrid = new GridPane();    
     
      //Setting size for the pane
      cnGrid.setMinSize(400, 200);
     
      //Setting the padding  
      cnGrid.setPadding(new Insets(10, 10, 10, 10));
      cnGrid.setVgap(5);
      cnGrid.setHgap(5);      
     
      //Setting the Grid alignment
      cnGrid.setAlignment(Pos.CENTER);
       
      //Arranging all the nodes in the grid
      cnGrid.add(text1, 0, 0);
      cnGrid.add(textField1, 1, 0);
      cnGrid.add(text2, 0, 1);      
      cnGrid.add(textField2, 1, 1);
      cnGrid.add(button1, 0, 2);
      cnGrid.add(button2, 1, 2);
       
      //Styling nodes  
      button1.setStyle("-fx-background-color: black; -fx-text-fill: white;");
      button2.setStyle("-fx-background-color: black; -fx-text-fill: white;");
       
      text1.setStyle("-fx-font: normal bold 20px 'serif' ");
      text2.setStyle("-fx-font: normal bold 20px 'serif' ");  
      cnGrid.setStyle("-fx-background-color: Black;");
       
      // Creating a scene object
      Scene scene = new Scene(cnGrid);
       
      // Setting title to the Stage  
      stage.setTitle("CSS Example");
         
      // Adding scene to the stage
      stage.setScene(scene);
     
      //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Using the following commands, compile and execute the stored java file from the command prompt.

javac JavaFXcss.java

java JavaFXcss

When run, the preceding software generates the JavaFX window shown below.

Output:

 

Example 2:

To create the inline CSS effect on provided components in JavaFX, we must import all of the necessary libraries. Then we extended the Application class to create a new class called CN_JavaFX_CSS. The following programme is an example of how to apply styles to the preceding application in JavaFX.

Css.java

import javafx.application.Application;    
import javafx.scene.Scene;    
import javafx.scene.control.Button;    
import javafx.scene.control.Label;    
import javafx.scene.control.TextField;    
import javafx.scene.layout.GridPane;    
import javafx.stage.Stage;  
 
public class CN_JavaFX_CSS extends Application {  
 
@Override  
public void start(Stage primaryStage) throws Exception {    

Label fname=new Label("First Name");    
Label lame=new Label("Last Name");    
Label country=new Label("Country Name");    
Label lang=new Label("Language Name");    
TextField tf1=new TextField();    
TextField tf2=new TextField();    
TextField tf3=new TextField();    
TextField tf4=new TextField();    

//styling the country, language, firstName and lastName attributes.
country.setStyle("-fx-background-color:Yellow; -fx-text-fill:black; -fx-font-size:16");  
lang.setStyle("-fx-background-color:Yellow; -fx-text-fill:black; -fx-font-size:16");  
fname.setStyle("-fx-background-color:Yellow; -fx-text-fill:black; -fx-font-size:16");  
lame.setStyle("-fx-background-color:Yellow; -fx-text-fill:black; -fx-font-size:16");  

//styling the submit button.
Button Submit=new Button ("Submit");    
Submit.setStyle("-fx-background-color:Red; -fx-text-fill:black; -fx-font-size:26");  


GridPane root=new GridPane();    
root.setHgap(10);  
root.setVgap(15);  
Scene scene = new Scene(root,600,400);
root.addRow(0, fname,tf1);  
root.addRow(1, lame,tf2);    
root.addRow(2, country,tf3);    
root.addRow(3, lang,tf4);    
root.addRow(4, Submit);    
primaryStage.setScene(scene);    
primaryStage.setTitle("Employee Information");
primaryStage.show();    
}    
public static void main(String[] args) {    
launch(args);    
}    
}

 

Output:

 

Explanation:

We must import all necessary libraries in JavaFX before we can apply the inline CSS effect to the specified components. Then we extended the Application class by creating a class called CN_JavaFX_CSS. In order to provide implementation details, we also need to override the start method. This function generates a primaryStage object from a Stage object. A Group object is constructed and then handed to the Scene class object in order to establish the container for holding numerous components with inline CSS effects.

The show() method is used to display output when the stage has been set, and the title has been chosen. The launch(args) method is invoked in the main() method to start the application. Employee Information is displayed in an output frame-like container. Additionally, a submit button is displayed along with labels for the first name, last name, country, and language.
 

Frequently Asked Questions

Is CSS supported by JavaFX?

Every JavaFX application always uses the default CSS. To replace the JavaFX default styles, you can develop one or more custom stylesheets of your own and include them in your application.

Where do I put CSS in JavaFX?

With the aid of the setStyle() method, in-line styles can also be added. These styles are relevant to the nodes on which they are set and are made up solely of key-value pairs.

What is inset JavaFX?

The JavaFX Insets class is a component. The interior offsets for each of the four sides of the rectangular region are stored in the Insets class. The Insets class derives from Java.

Conclusion

In this blog, we have extensively discussed the JavaFX Inline Styles. We hope that this article has helped all of you with additional information about the JFX styling. And to learn in-depth about Structure-execution, check out the course on our JavaFX on the Coding Ninjas website. For more information about inline styling and JavaFX, you can check out JavaFX-CodingNinjas and Inline CSS
Also, take a look at the Coding Ninjas website for some great information, Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog to help other ninjas grow.

Delighted Programming!

Live masterclass