I've got a MenuBar that is setup as follows in FXML:
<MenuBar VBox.vgrow="NEVER">
<menus>
<Menu mnemonicParsing="true" text="_File">
<items>
<MenuItem mnemonicParsing="true" text="_New Project"/>
<MenuItem mnemonicParsing="true" text="_Open…"/>
<MenuItem mnemonicParsing="false" text="Quit"/>
</items>
</Menu>
</menus>
</MenuBar>
This produces a menu as follows:
I've successfully styled the MenuBar
and the Menu
File with the following CSS:
.menu-bar { /* The menu bar itself */ }
.menu { /* The File menu item */ }
.menu:showing { /* menu when it's being shown (activated) */ }
.menu .label { /* Styles the text on a menu item */ }
.menu:showing .label { /* Styles the text on a menu item when activated */ }
However, I've been unable to style the menu that is displayed.
I've tried treating it as a ContextMenu:
.context-menu {
-fx-background-color: red;
}
Doesn't do anything (it's not a ContextMenu, so no big surprise here).
I've tried styling menu-item
and menu-button
:
.menu-button,
.menu-item {
-fx-background-color: red;
}
This changes the menu (File), but not the menu items or the menu that is displayed.
I've tried selecting a substructure called .items
but that doesn't seem to exist.
To help clarify which elements I'm looking to style, I've added this image which outlines the components I'm wishing to style:
The default css is always applied to every JavaFX application. However, you can create one or more custom stylesheets of your own and add them to your application to override the default styles defined by JavaFX.
In Scene Builder, you can simulate the attachment of a style sheet to an application Scene by selecting Preview, then Scene Style Sheets, and finally choosing Add a Style Sheet or Open a Style Sheet option. This Preview command is useful when the “root” style class is defined in the style sheet.
I think you forgot the -fx-skin
property in .context-menu
.
Follow the How to style menu button and menu items.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With