JMetro – Windows 8 Metro controls on Java

[Update 12 March, 2018: You should check out the Java, JavaFX theme page for updated information, things have changed]

Hi,

I’ve been studying the interface of Windows 8, so I thought why not do a windows 8 theme for Java and what better timing than this given that there are only 3 days left for windows 8 to be available.

My first control is the Windows 8 “Push Button”, it’s simply just the normal button that you’re used to.

I’ll be releasing two themes: a dark theme (JMetroDarkTheme.css) and a light theme(JMetroLightTheme.css). The GIF below was captured from the demo aplication.

JavaFX Button with JMetro Dark Theme

Push Button – JMetro Dark Theme

JavaFX Button with JMetro Light Theme

Push Button – JMetro Light Theme

Together with the css files I’ve put an fxml file as an example. You can open it in Scene Builder then choose the style sheet you want to use by pressing “Preview -> Preview a style sheet…”. To see it in action click “Preview->Preview in window”.

The buttons are simply styled by adding the “.push-button” css class to a Button.

You can get the files here. Use them for any purpose, commercial or not.

Zooming inside a Scrollpane

In JavaFX if you want the user to be able to zoom the contents inside a scrollpane here’s a small gotcha that you have to be aware of: to accomplish this you must wrap the contents of the scroll node inside a group so that the visual bounds (not the layout bounds) are used for layout calculations.

Scrolling can than be attained by setting a scaling transform on the contents of the Scrollpane and adjusting the scale value to meet the specified zoom level.

Here’s a small snippet of code to illustrate how you could do it:

public class ZoomableScrollPane extends ScrollPane{
  Group zoomGroup;
  Scale scaleTransform;
  Node content;
  (…)
  public ZoomableScrollPane(Node content)
  {
    this.content = content;
    Group contentGroup = new Group();
    zoomGroup = new Group();
    contentGroup.getChildren().add(zoomGroup);
    zoomGroup.getChildren().add(content);
    setContent(contentGroup);
    scaleTransform = new Scale(scaleValue, scaleValue, 0, 0);
    zoomGroup.getTransforms().add(scaleTransform);
    (…)
  }
  (…)
}

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)

Target text on JavaFX UI controls through CSS

Here’s a small post on how you can target text on UI controls through CSS.

Just do this:

<control> .text
{
    /* CSS targeting the text in the UI control */
}

To change the text appearance on any of the JavaFX controls.

Example (adding a drop shadow to the text on a button):

.myButton .text {
    -fx-effect: dropshadow( one-pass-box , rgba(100, 100, 100, 0.5) , 0, 0.0 , 0 , 1 );
}

Result:

I think this is a very nice way to style text on UI controls, one which is not available on regular “Web HTML CSS”.