Toni Epple works as a consultant for Eppleton (http://www.eppleton.de) in Munich, Germany. In his spare time he's an active member of the Open Source community as a community leader for JavaTools community (http://community.java.net/javatools/), moderator of the XING NetBeans User Group (http://www.xing.com/group-20148.82db20), founder of the NetBeans User Group Munich (http://tinyurl.com/5b8tuu), member of the NetBeans Dream Team (http://wiki.netbeans.org/NBDTCurrentMembers) and blogger (http://www.eppleton.de/blog). Toni is a DZone MVB and is not an employee of DZone and has posted 51 posts at DZone. You can read more from them at their website. View Full User Profile

Styling a JavaFX Control with CSS

01.02.2012
| 35527 views |
  • submit to reddit

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.

Published at DZone with permission of Toni Epple, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Geertjan Wielenga replied on Sat, 2012/01/07 - 1:59pm

This is cool! Looking forward to styling my NetBeans Platform application with CSS...

Leonard Sitongia replied on Wed, 2012/01/18 - 5:08pm

Very nice, thanks.

In this, and the previous lesson, the runtime cannot find the CSS:

WARNING: com.sun.javafx.css.StyleManager loadStylesheet Resource "null" not found.

 

Toni Epple replied on Tue, 2012/01/24 - 5:46am

 Hi Leonard,

have you tried the NetBeans Project I uploaded, or did you implement it yourself? In the latter case make sure that getUserAgentStylesheet returns the correct path to the css file.

Sheraz Butt replied on Sun, 2012/05/20 - 11:03am

This is cool! Awaiting design my NetBeans Foundation program with CSS... trendsandgadgets.net

Sheraz Butt replied on Tue, 2012/06/19 - 7:49am

This is cool! Looking forward to style my NetBeans Groundwork system with CSS.. stonedinereviews.blogspot.com

sharksteammopreviews.net

Mateo Gomez replied on Wed, 2012/06/06 - 1:09am

i must admit i like the desing and look of this

mexican dessert

Matt Coleman replied on Thu, 2012/06/07 - 3:16am

I cant begin to explain this awesomeness!!thanks for sharing! graphic artist buffalo

 

Sheraz Butt replied on Mon, 2012/09/24 - 5:50am

Natty Shirts has been serving its customers with finest Custom Made Dress Shirts, reknown for its fine quality, elegant work and superb customer services. We believe in establishing long term relations for which we strive our best and make our customers for life.

Sheraz Butt replied on Sat, 2012/10/13 - 2:43am

This is cool! Awaiting design my NetBeans Foundation program with CSS... ...more ...more

Jason Bourne replied on Thu, 2012/10/18 - 12:46pm

This became truly very helpful site. I truly enjoyed what sort of written content had been published. I will be content to help save this web site in to our folder. Many thanks! The way you express yourself is awesome.Hey, your blog is great..
http://www.expocatadores.com.br

Jason Bourne replied on Thu, 2012/10/18 - 12:48pm

This became truly very helpful site. I truly enjoyed what sort of written content had been published. I will be content to help save this web site in to our folder. Many thanks! The way you express yourself is awesome.Hey, your blog is great.. jailbreak my iphone 3gs

Jason Bourne replied on Mon, 2012/11/19 - 9:11am

 
Took me time to read all the comments, but really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article
reinigingsmachinesrs

Siddle Gibbs replied on Sat, 2012/12/01 - 11:58am in response to: Sheraz Butt

This subject has impressed me for quite some time. I have just started researching it on the Internet and found your post to be informative MMA Gear 

Jason Bourne replied on Fri, 2012/12/14 - 6:26am

 

It entails considerably valuable facts for me. I am pleased to locate your distinguished technique of producing the publish. Now you allow it for being uncomplicated for me to understand and implement the principle. Thank you on your fit up.
cupping set

Danish12 Ali replied on Wed, 2013/06/05 - 5:53am

 a regeneration is at last underway in the Syrian-controlled Golan Heights as its people struggle to carve out their lives amidst the threats, promises and scars of occupation"
then I don’t want you writing journalism or judging it Miami Male Strippers

Danish12 Ali replied on Wed, 2013/06/05 - 7:15am

 I personally have embraced the new technologies and the CMS platforms, I think the new tools only make the web designs better. bail bonds in dallas tx

Danish12 Ali replied on Thu, 2013/08/29 - 4:58am

 I've just decided to create a blog, which I have been wanting to do for a while. Thanks for this post, it's really useful!  window tinting Houston

Danish12 Ali replied on Sat, 2013/08/31 - 5:33am

 I'm happy to capture your article and I am looking forward to more of your article. Superb! Generally I never read whole articles but the way you wrote this information is simply amazing and this kept my interest in reading and I enjoyed it  Unlawful detainer in california

Uchenna Ani-Okoye replied on Thu, 2013/10/10 - 8:51am

Nice datechooser, personally I like to go with a more basic yet sleek design, hopefully I'll be able to get that with the code you have given. - Rd Okoye

Abbey Zabby replied on Sun, 2013/10/20 - 12:54am in response to: Matt Coleman

I like your writing style. A little bit humour mixed with a useful information. It's exactly what you need to attract a visitor! I am happy that I've found your web page and I will definitely share it with my friends too Car Shipping FAQ’s

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.