DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Why Tailwind CSS Can Be Used Instead of Bootstrap CSS
  • The Ultimate Guide to Code Formatting: Prettier vs ESLint vs Biome
  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Loader Animations Using Anime.js

Trending

  • Threat Modeling for Developers: Identifying Security Risks in Software Projects
  • The Scrum Guide Expansion Pack
  • It’s Not Magic. It’s AI. And It’s Brilliant.
  • Modernize Your IAM Into Identity Fabric Powered by Connectors
  1. DZone
  2. Coding
  3. Java
  4. Styling a JavaFX Control with CSS

Styling a JavaFX Control with CSS

Change the look and feel of any JavaFX control using CSS.

By 
Toni Epple user avatar
Toni Epple
·
Jan. 02, 12 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
96.5K Views

Join the DZone community and get the full member experience.

Join For Free

Last week I wrote about how to create your own JavaFX control, using a DatePicker as an example. What is especially cool is that you can easily change the look and feel of any JavaFX control using CSS. For that purpose, we applied CSS-styles to several nodes in our component.

In this article, we'll override those styles to achieve a different look and feel of our component. I created a NetBeans project with the source code. You can download the source code for this example here:

DateChooser.zip

For my Eppleton-branded DateChooser I wanted to have a "LCD-calendar" theme and my logo as a background. Here are two before and after images of our control:

Before: The default style as defined in calendar.css.

After: Our new LCD-style. The base style class we defined is "date-chooser".

In our default css it only defines the -fx-skin we want to use. Let's start by creating a new css file named lcd.css and change the TestApplication class to use it:

scene.getStylesheets().add(TestApplication.class.getResource("lcd.css").toExternalForm());

The first thing we'll add is a simple gradient for the background.lcd.css

.date-chooser {
    -fx-background-color:linear-gradient(white,#DDDDDD);
}

As our next step we'll add a background image and move it to the bottom left. Since by default it would be repeated, we'll have to forbid that:

.date-chooser {
     ...
    -fx-background-image: url("images/background.png");
    -fx-background-repeat: no-repeat;
    -fx-background-position: left bottom;
}

Now you can see our background image behind the calendar cells:

For a cleaner look, I'd like to get rid of the background of unselected cells. So we'll change the calendar-cell to use an invisible background. I still wanted the background to change on hovering and I wanted to mark the selected date and the current date with a different background. That's why I define the background-radius and background-insets in my base class, even though we don't see it.

The text color will be slightly transparent because of the background image. As the last step we set Arial as the font and add a drop shadow effect for the LCD-look on all labels. Since our calendar-cell is derived from a label it will inherit these changes:

.label {    
     -fx-effect: dropshadow(two-pass-box , darkgray, 10, 0.0 , 4, 5);     
}

.calendar-cell { 
    -fx-background-color: rgba(255,255,255,0.0);
    -fx-background-radius: 15 15 15 15;
    -fx-background-insets: 6 6 6 6 ;
    -fx-text-fill: rgba(0,0,0,0.7);
    -fx-font: 16pt Arial;
}

This is the result:

Now let's change the background and text-fill for hovered, pressed, selected cells and for the one representing the current date (calendar-cell-today). calendar-cell-inactive are cells in the previous or following month that might fall in the first or last week of the month currently displayed.:

.calendar-cell:hover { 
    -fx-background-color: rgba(135,206,235,0.7);
    -fx-text-fill: white;   
}

.calendar-cell:pressed { 
    -fx-background-color: rgba(135,186,215,0.7);
    -fx-text-fill: white;   
}

.calendar-cell-selected { 
    -fx-background-color: rgba(105,176,195,0.7);
    -fx-text-fill: white;   
}

.calendar-cell-today { 
    -fx-background-color: rgba(155,155,19,0.7);
    -fx-text-fill: white;
}

.calendar-cell-inactive { 
    -fx-text-fill: darkgray;
}

This almost looks like what I wanted. I only need to change the buttons a bit, to add the drop shadow and flatten them a bit:

.button{
    -fx-effect: dropshadow(two-pass-box , darkgray, 10, 0.0 , 4, 5);
    -fx-background-color: rgba(205,205,205,0.7);
    -fx-background-radius: 15 15 15 15;
    -fx-background-insets: 2 2 2 2 ;
}

.button:pressed{
    -fx-background-color: rgba(180,180,180,0.7);
}

.button:hover{
    -fx-background-color: rgba(235,235,235,0.7);
}

.button:focused{
    -fx-background-color: rgba(205,205,205,0.7);

}

...and we're done:

I really like the fact that you can totally change the appearance of a component like this via CSS. Now compare the effort it took to make this component styleable to the effort you would have to change it's appearance in Swing. The CSS-support really is one of my favorite features of JavaFX.

CSS JavaFX

Opinions expressed by DZone contributors are their own.

Related

  • Why Tailwind CSS Can Be Used Instead of Bootstrap CSS
  • The Ultimate Guide to Code Formatting: Prettier vs ESLint vs Biome
  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Loader Animations Using Anime.js

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: