Java Reference
In-Depth Information
items to allow the user to choose the preferred Look and Feel. When creating a menu
item, you'll create a convenience method to build a menu item that loads the specified
CSS and an
EventHandler
action, via a lambda expression, to apply the chosen
CSS style to the current UI. The Modena look and feel is loaded by default. Different
look and feels can be applied by passing their respective stylesheets to the
setUser-
AgentStylesheet()
method. For instance, to load the Caspian Look and Feel, you
simply pass the constant
STYLESHEET_CASPIAN
to the
setUserA-
gentStylesheet()
method. The following code shows how to create these menu
items:
MenuItem caspianLnf = new MenuItem("Caspian");
caspianLnf.setOnAction(skinForm(caspian, scene));
Shown next is the code for adding a menu item containing the Sky Look and Feel
CSS style, which is ready to be applied to the current UI.
// New Modena Look and Feel
MenuItem modenaLnf = new MenuItem("Modena");
modenaLnf.setOnAction(enableCss(STYLESHEET_MODENA,scene));
menu.getItems().add(modenaLnf);
The
setOnAction()
method calls a method named
enableCss()
, which
takes a style sheet and the current scene. The code for
enableCss()
is as follows:
protected final EventHandler<ActionEvent>
enableCss(String style, final Scene scene){
return (ActionEvent event) -> {
scene.getStylesheets().clear();
setUserAgentStylesheet(style);
};
}
For each of the other CSS styles, which are not part of the default JavaFX distribu-
tion, the menu item creation is a bit different. This is an example of the code that util-
izes the convenience method that was previously discussed.
menu.getItems().add(createMenuItem("Control Style 1",
"controlStyle1.css", scene));