How to Style a Javafx Menu and Its Items in CSS

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

I think you forgot the -fx-skin property in .context-menu.

Follow the How to style menu button and menu items.

How to style menu button and menu items

MenuButton uses Menu internally and has a similar API. In such way that MenuButton contains MenuItems list just like Menu. So I think you need to try to play with .menu, .menu-button and .menu-item CSS selectors in caspian.css. More specifically with .menu-item.

EDIT: It seems you need to change the .context-menu too, because the popped up menu of the menuButton is ContextMenu.

.menu-item .label {
-fx-text-fill: white;
}

.menu-item:focused {
-fx-background-color: darkgray;
}

.menu-item:focused .label {
-fx-text-fill: blue;
}

.context-menu {
-fx-skin: "com.sun.javafx.scene.control.skin.ContextMenuSkin";
-fx-background-color: black;
-fx-background-insets: 0, 1, 2;
-fx-background-radius: 0 6 6 6, 0 5 5 5, 0 4 4 4;
/* -fx-padding: 0.666667em 0.083333em 0.666667em 0.083333em; 8 1 8 1 */
-fx-padding: 0.333333em 0.083333em 0.666667em 0.083333em; /* 4 1 8 1 */
}

JavaFX CSS - Font color in MenuItem

To style the text of the menu-item in css, you have to select the label of the menu-item using .menu-item .label{....} like,

.menu-item .label{
-fx-text-fill: greenyellow;
}

I hope this solved your problem.

JavaFX css menu borders wrong color

If you have 1 context-menu on the scene at the same time:

I succeeded to change the backgroung color of Menu node to red by adding this code to the style sheet:

.context-menu {
-fx-background-color: #ff0000;
-fx-border-color: #ff0000;
}

I suppose you are attaching your style sheet to the .fxml file, if no, you can do it by specifying the path in SceneBuilder:

Sample Image

If you have 2 context-menu on the scene at the same time:

Just specify 2 style sheets and then attach them sepparately for different MenuBar nodes like on screenshot above. In each style sheet specify .context-menu class with desired properties. This way it won't override each other.

How do you set the style for a JavaFX ContextMenu using css?

Here is a simple example of styling a JavaFX context menu via css.

Tested on WinXPsp3, Jdk7u6b14ea, JavaFX 2.2b12.

java app

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;

public class ContextColor extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) {
Label label = new Label("Right click for options");
label.setContextMenu(new ContextMenu(new MenuItem("Help"), new MenuItem("Me")));
Scene scene = new Scene(label);
scene.getStylesheets().add(ContextColor.class.getResource("contextcolor.css").toExternalForm());
stage.setScene(scene);
stage.show();
}
}

css stylesheet

/** contextcolor.css
* place in same folder as ContextColor.java
* ensure your build system copies this file to the ContextColor.class output directory on build
*/
.root {
-fx-background-color: cornsilk;
-fx-padding: 10;
}

.context-menu {
-fx-background-color: #006699;
-fx-text-fill: white;
}

.menu-item .label {
-fx-text-fill: yellow;
}

.menu-item:focused .label {
-fx-text-fill: white;
}

I couldn't tell you the exact reason why your css styling did not function as you expect.
Some possible reasons are:

  1. You are not loading it correctly.
  2. Your css file is not copied to your output path.
  3. Your css file is otherwise corrupted or syntactically incorrect.
  4. You are using an earlier version of JavaFX which has difficulty styling context menus from css.

Update

Looking at the complete code in your question where the css file is loaded via fxml, I can reproduce your issue where the context menu is not styled. If, instead of setting the stylesheet in the fxml, I set the stylesheet on the scene in code (as in my test app), then it all works fine.

The difference when the css is set via fxml is that the fxml is not setting the stylesheet on the scene, but instead on the parent root node of the scene. If in the code I add the stylesheet to the parent rather than the scene, then I end up with the same behaviour from the code implementation as the fxml. So this is not really an issue with fxml per se, but rather it is in issue with the inheritance rules of the JavaFX 2.2 css processing. IMO, the css processing is wrong - the styling should be the same whether the stylesheet has been set on the scene or on the root node of the scene.

I advise filing a bug against the JavaFX runtime controls at http://javafx-jira.kenai.com with your test case and a link back to this StackOverflow question and the JavaFX team will resolve the issue in due time.

As a workaround, just set your stylesheet on the scene in code for now.


Update

Root cause for this issue appears to be RT-19435: popup control not styled be parent's style sheet declarations.

Menu button style while menu open JavaFX

The documentation indicates that MenuButton has a showing pseudoclass that is set when the context menu is showing, so you can do

.menu-button:showing {
-fx-base: black ;
}

Here's a quick test harness:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class App extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
MenuButton button = new MenuButton("Test");
button.getItems().addAll(
new MenuItem("Item 1"),
new MenuItem("Item 2")
);
BorderPane root = new BorderPane();
root.setTop(button);
Scene scene = new Scene(root, 200, 200);
scene.getStylesheets().add(getClass().getResource("/style.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

from which I got the following:

Sample Image

and

Sample Image

Edit MenuBar with Css in JavaFx

Make:

.menu .label{     -fx-text-fill: black;}

Reference: https://forums.oracle.com/message/10402062



Related Topics



Leave a reply



Submit