Platinum Partner
java,javafx,tips and tricks,client-side

Style a Stage in a Minimalist Way

There are various modes in which you can style a window in JavaFX. Sometimes it may be interesting to style it in a minimalist way without any window decorations but only the ones provided by the OS such as a drop shadow in Windows 7.

First lets take a look at what JavaFX gives you. There are 4 ways to style your Stage:

  • StageStyle.DECORATED – A stage with solid white background and platform decorations.

  • StageStyle.UNDECORATED – A stage with a solid white background and no decorations.

This is basically just a Stage with no decorations at all just a solid white rectangle with no borders  for you to paint on.

  • StageStyle.TRANSPARENT – A stage with a transparent background and no decorations.

Same as the above but with a transparent background instead of solid white.

  • StageStyle.UTILITY – A stage with a solid white background and minimal platform decorations.

This one is similar to the StageStyle.DECORATED expect that the corners aren’t round, there is no minimize and maximize button and the close button is smaller. Also it does not show up on the windows taskbar so it is not meant to be a top level application window but a secondary one.

So there is no provided way to style a top level window with only the OS decorations. To achieve this you’ll have to create a window with no decorations using StageStyle.UNDECORATED and style the top level container through CSS.

The following code sets the top level container borders as round and adds a drop shadow to achieve the windows 7 appearance:

-fx-background-radius: 6;

-fx-border-color: gray;

-fx-border-style: solid;

-fx-border-width: 1;

-fx-border-radius: 6;

-fx-background-radius: 6;

-fx-effect: dropshadow(three-pass-box, rgba(100, 100, 100, 1), 24, 0.5, 0, 0);

However, there is a problem with this code. The drop shadow will not be visible since it falls outside the bounds of the scene. For it to be visible you’ll have to provide some space around the top level container, you can do this using -fx-background-insets  which sets the insets of the container and -fx-border-insets which sets some space around the borders as well.

So the final CSS for a top level container with a style class of rootPane will look like this:

.rootPane

{

  -fx-border-insets: 23;

  -fx-background-insets: 23;

  -fx-background-radius: 6;

  -fx-border-radius: 6;

  -fx-border-color: gray;

  -fx-border-style: solid;

  -fx-border-width: 1;

  -fx-effect: dropshadow(three-pass-box, rgba(100, 100, 100, 1), 24, 0.5, 0, 0);

}

And the window will look like this:

(The logo, top header and close button are later additions, i.e. not a result of the CSS code above)

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}