Java Reference
In-Depth Information
Calling the createMenuItem() method will also call another convenience
method to load the CSS file called loadSkin() . It will also set the menu item's
onAction attribute with an appropriate EventHandler by calling the
skinForm() method. To recap, the loadSkin is responsible for loading the CSS
file, and the skinForm() method's job is to apply the skin onto the UI application.
Shown here are the convenience methods to build menu items that apply CSS styles to
a UI application:
protected final MenuItem createMenuItem(String label,
String css, final Scene scene){
MenuItem menuItem = new MenuItem(label);
ObservableList<String> cssStyle = loadSkin(css);
menuItem.setOnAction(skinForm(cssStyle, scene));
return menuItem;
}
protected final ObservableList<String>
loadSkin(String cssFileName) {
ObservableList<String> cssStyle
= FXCollections.observableArrayList();
cssStyle.addAll(getClass().getResource(cssFileName).toExternalForm());
return cssStyle;
}
protected final EventHandler<ActionEvent> skinForm
(final ObservableList<String> cssStyle, final Scene
scene) {
return (ActionEvent event) -> {
scene.getStylesheets().clear();
scene.getStylesheets().addAll(cssStyle);
};
}
Note To run this recipe, make sure the CSS files are located in the compiled classes
area. Resource files can be loaded easily when placed in the same directory (package) as
the compiled class file that is loading them. The CSS files are co-located with this code
Search WWH ::




Custom Search