How can I style a JavaFX menu and its items in CSS?
Asked Answered
F

2

14

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:

enter image description here

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.

Questions

  1. How do I select/style the menu (the container that is holding New Project, Open..., Quit)?
  2. How do I select/style each individual MenuItem in the menu?

Clarification

To help clarify which elements I'm looking to style, I've added this image which outlines the components I'm wishing to style:

enter image description here

Figured answered 20/2, 2014 at 15:55 Comment(2)
your question helped me more than the answers, thx ;)Siderostat
Possible duplicate of How to style menu button and menu itemsAjax
B
7

I think you forgot the -fx-skin property in .context-menu.
Follow the How to style menu button and menu items.

Bowne answered 21/2, 2014 at 11:30 Comment(2)
This does seem to fix the problem. Thanks so much. Can you explain a little bit about why the -fx-skin property is required?Figured
@crush: It is not necessary to use -fx-skin, see my answer. But still, if you are still interested, the official documentation says that -fx-skin defines The class name of the Control's Skin.Ajax
R
3
.menu-bar {
    -fx-background-color: black ;
    -fx-opacity: 0.5;
    -fx-border-width: 2.0;

}
.menu-bar .label {
    -fx-font-size: 14.0 pt;
    -fx-font-family: "Bookman Old Style";
    -fx-text-fill: white;
    -fx-font-weight: bold; 
    -fx-padding: 10.0px;

}
.menu-bar .menu-button:hover, .menu-bar .menu-button:focused, .menu-bar .menu-button:showing {
    -fx-background: black ;
    -fx-opacity: 0.5;
    -fx-text-fill: white;
}

.menu-item {
    -fx-padding: 0.0em 0.0em 0.0em 0.0em;
    -fx-text-fill: black;
    -fx-background: darkgray ;
}
.menu-item .label{
    -fx-font-size: 14.0 pt;
    -fx-text-fill: black;
}
.menu-item .label:hover{
    -fx-font-size: 14.0 pt;
    -fx-background: black ;
    -fx-text-fill: white;
}
.context-menu {
    -fx-skin: "com.sun.javafx.scene.control.skin.ContextMenuSkin";
    -fx-background-color:white ;
    -fx-border-width: 0.2px;
    -fx-border-color: black;
    /** -fx-background-insets: 0.0, 1.0, 2.0;
    -fx-background-radius: 0.0 6.0 6.0 6.0, 0.0 5.0 5.0 5.0, 0.0 4.0 4.0 4.0;
    -fx-padding: 0.333333em 0.083333em 0.666667em 0.083333em; 
    -fx-opacity: 0.9;*/
}
Repressive answered 17/3, 2020 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.