JavaFX CSS - JavaFX

What is JavaFX CSS?

Cascading style Sheets, also called CSS, is a easy layout language supposed to simplify the method of creating web pages presentable.

CSS handles the look and feel part of a web page. using CSS, you may manage the color of the text, style of fonts, spacing among paragraphs, size of columns and format. apart from those, you may additionally manage the background images or shades which might be used, format designs, variations in show for specific devices and screen sizes as well as a variety of other effects.

CSS in JavaFX

JavaFX presents you the facility of the use of CSS to enhance the appearance and feel of the application. The package javafx.css includes the lessons which are used to use CSS for JavaFX applications.

A CSS contains of style rules which can be interpreted by using the browser after which applied to the corresponding factors to your file.

A style rule is made from three parts, that are −

  • Selector − A selector is an HTML tag at which a style can be carried out. this will be any tag like or<h1> or <table>, etc.
  • Property − A assets is a sort of characteristic of the HTML tag. In easier terms, all the HTML attributes are converted into CSS properties. They can be color, border, etc.
  • Value − Values are assigned to properties. for example, a colour assets will have price either red or #F1F1F1, etc.

You may placed CSS style Rule Syntax as follows –


The default style sheet used by JavaFX is modena.css. it is located within the JavaFX runtime jar.

Adding Your Own Style Sheet

You may upload your own style sheet to a scene in JavaFX as follows –

Adding Inline Style Sheets

You can also upload in-line styles the usage of the setStyle() technique. these styles include only key-price pairs and they are relevant to the nodes on which they may be set. Following is a sample code of placing an inline style sheet to a button.


Assume that we have advanced an JavaFX application which displays a form with a text subject, Password area, Buttons. through default, this shape seems as shown inside the following screenshot –


The following application is an example which demonstrates how to add styles to the above application in JavaFX.

Save this code in a record with the name

Collect and execute the save java file from the command prompt using the following commands.

On executing, the above program generates a JavaFX window as shown below.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

JavaFX Topics