Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

JavaFX CSS Tutorial for beginners | 100% best for beginners

September 7, 2021 - Updated on October 29, 2021
in Java
Reading Time: 4 mins read
0
JavaFX CSS Tutorial
1.4k
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • JavaFX CSS Tutorial
    • Easiest methods to add CSS to a JavaFX application
  • How to add CSS to JavaFX
    • Example: CSS Styling
    • Example: Adding CSS in JavaFX
    • Output
    • Example: Clear the CSS in JavaFX
  • Styling specific objects using ID selector
  • Applying CSS styles to JavaFX nodes
    • Example
    • Output
  • Applying CSS Style using SceneBuilder
  • Applying CSS file using SceneBuilder
  • YouTube Video

JavaFX CSS Tutorial

Cascading Style Sheets (CSS) is a style sheet language used to design applications to make it looks great. In this article, we will show you methods and examples for applying Cascading Style Sheets (CSS) into your JavaFX application. This JavaFX CSS tutorial is very simple to learn because we will use the simplest and easiest method to add CSS to your JavaFX application.

Easiest methods to add CSS to a JavaFX application

  1. Applying CSS using a CSS file
  2. Applying CSS to nodes
  3. Using the SceneBuilder

How to add CSS to JavaFX

To add CSS to your JavaFX application, we will use the simplest and easiest methods to add CSS in JavaFX application. In our first example, we will add a CSS file to your JavaFX application and change the styles of the scene’s buttons and root node. To identify a UI element are called selectors and most CSS statement looks as follows.

.selector-name {
    property-name1: value1;
    property-name2: value2;
}

Note that all JavaFX CSS properties must have the -fx prefix. The following example will be styling the root node of our JavaFX application.

Example: CSS Styling

This is how to add styles in a CSS file. The .root refers to the root element of the scene and .button refers to all instances of the Button class. If you will use the .button selector it means that the style will be applied to all buttons in your scene.

.root{
    -fx-background-color: lightblue;
}

.button{
    -fx-background-color: black;
    -fx-text-fill: white;
}

Example: Adding CSS in JavaFX

Now, let’s use this CSS in the application. We will then use the root variable to add CSS to the application. Note that the "style.css" is the filename of the CSS file. You can put this code anywhere you want, such as the button action event or the main class.

root.getStylesheets().add(getClass().getResource("style.css").toExternalForm());

Output

To trigger the style, I put the code at the button action event to fire the CSS in JavaFX.

JavaFX CSS Tutorial

Example: Clear the CSS in JavaFX

Clear the JavaFX CSS tutorial, If you want to remove the loaded CSS in your application, you can easily do this. See the example code below.

root.getStylesheets().clear();

Styling specific objects using ID selector

Addressing specific nodes of the JavaFX classes can be done with the # symbol. If you have two buttons in your JavaFX application and want to style one of them while leaving the other button alone. You can easily do this by looking the code example below.

// JavaFX Code
Button button1 = new Button("Learning with Kensoft PH");
button.setID("button1");
/* CSS */
#button1 {
    -fx-background-color: black;
    -fx-text-fill: white;
}

Applying CSS styles to JavaFX nodes

Loading a CSS file is not the only option to work with styles. In this JavaFX CSS Tutorial you can also load them directly to the JavaFX Code. Using the setStyle() method, the method can be called directly from the JavaFX Code to apply the CSS style to a specific nodes. Adding CSS to node looks like this. Node.setStyle(String style)

Example

In this demonstration, we will use a button to set the style of the background color, text color, width, height, and font size using the setStyle() method. See the code example below.

@FXML
    private void Button_Style(ActionEvent event) {
        btn_style.setStyle("-fx-background-color:"+text_bg.getText()+";"+
                "-fx-text-fill:"+text_color.getText()+";"+
                "-fx-min-height:"+text_height.getText()+";"+
                "-fx-min-width:"+text_width.getText()+";"+
                "-fx-font-size:"+text_font.getText()+";");
    }

Output

JavaFX CSS Tutorial

Applying CSS Style using SceneBuilder

Using SceneBuilder is one of the simplest ways to apply CSS style in JavaFX. To set the style of a node in SceneBuilder, select any nodes you want to set the style and go to its properties, scroll down, and look for Style. I will provide a screenshot below to see how it looks. The first textfield is for the style property and the second one is for the value.

css style

Applying CSS file using SceneBuilder

JavaFX CSS tutorial, applying CSS file using SceneBuilder. Create a CSS file like a method we discussed above and create CSS style on it. Once you already created and style it. Go to your SceneBuilder, select any node, and then go to its properties. Look for Stylesheets and add the CSS file.

If your Selector refers to all instances of the same class, you don’t need to choose anything from the Style Class. However, if your Selector has a specific style for a specific node, you need to select your specific style from the Style Class.

css style

YouTube Video

YouTube video

Previous article

Tags: CSS in JavaFXCSS StyleJavaFX CSSJavaFX CSS Tutorial
Previous Post

How to get the window size in JavaFX | 100% best tutorial

Next Post

ScrollPane in JavaFX | 100% best tutorial for beginners

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
216
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
590
Next Post
ScrollPane in JavaFX

ScrollPane in JavaFX | 100% best tutorial for beginners

JavaFX Effects Tutorial | 100% best for beginners

JavaFX Effects Tutorial | 100% best for beginners

JavaFX WebView

JavaFX WebView Tutorial | 100% best for beginners

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

    How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! I'm Kent. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2024 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.