Changing the display indicator of the dropdown menu in the JavaFX MenuButton?

In my application, I use MenuButton to provide a drop down list of actions. The default drop indicator on the MenuButton is a black triangle pointing down. I want to change this to a white triangle pointing down to match the color of my text.

Just in case, I don’t understand, here is a screenshot that should clear it.

black triangle

I tried to place the graphics in the fxml file as follows:

 <MenuButton contentDisplay="RIGHT" graphicTextGap="10.0" layoutX="92.0" layoutY="73.0" mnemonicParsing="false" styleClass="toolbar-button" text="MenuButton"> <graphic> <ImageView fitHeight="4.0" fitWidth="7.0" mouseTransparent="true" preserveRatio="true"> <image> <Image url="@Arrow_Down.png" preserveRatio="true" smooth="false" /> </image> </ImageView> </graphic> <items> <MenuItem mnemonicParsing="false" text="Action 1" /> <MenuItem mnemonicParsing="false" text="Action 2" /> </items> </MenuButton> 

but it gives me a black and white triangle:

white and black triangle

If I could somehow hide the black triangle that would work, but there seems to be a way to stylize the menu button so that it is white.

Here is an example of Sample.fxml for those who want to help:

 <?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.net.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.image.*?> <?import javafx.scene.layout.*?> <AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml"> <children> <MenuButton contentDisplay="RIGHT" graphicTextGap="10.0" layoutX="92.0" layoutY="73.0" mnemonicParsing="false" styleClass="toolbar-button" text="MenuButton"> <graphic> <ImageView fitHeight="4.0" fitWidth="7.0" mouseTransparent="true" preserveRatio="true"> <image> <Image url="@Arrow_Down.png" preserveRatio="true" smooth="false" /> </image> </ImageView> </graphic> <items> <MenuItem mnemonicParsing="false" text="Action 1" /> <MenuItem mnemonicParsing="false" text="Action 2" /> </items> </MenuButton> </children> <stylesheets> <URL value="@test.css" /> </stylesheets> </AnchorPane> 

Test.css:

 root { display: block; } .toolbar-button { -fx-background-color: #006699; -fx-padding: 2 4 4 4; -fx-text-base-color: #FFFFFF; -fx-font-weight: bold; -fx-font-size: 12px; } .toolbar-button:hover { -fx-background-color: #B2E1FF; -fx-padding: 2 4 4 4; -fx-text-base-color: #000000; -fx-font-weight: bold; -fx-font-size: 12px; } 

And Test.java to run:

 package test; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Test extends Application { public static void main(String[] args) { Application.launch(Test.class, args); } @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml")); stage.setScene(new Scene(root)); stage.show(); } } 

How can I make a black triangle a white triangle?

+4
source share
1 answer

After some digging, I found the answer. Turns off MenuButton does not use the image for the button, but instead we have the -fx-shape css property for this. In the caspian.css file, it is used in the .menu-button .arrow and .menu-button:openvertically .arrow . Since I already applied toolbar-button to my MenuButton, I just added the following to my css file:

 .toolbar-button .arrow { -fx-background-insets: 1 0 -1 0, 0; -fx-background-color: -fx-mark-color, #FFFFFF; -fx-padding: 0.25em; /* 3 */ -fx-shape: "M 0 -3.5 v 7 l 4 -3.5 z"; } .toolbar-button:hover .arrow { -fx-background-insets: 1 0 -1 0, 0; -fx-background-color: -fx-mark-highlight-color, -fx-mark-color; -fx-padding: 0.25em; /* 3 */ -fx-shape: "M 0 -3.5 v 7 l 4 -3.5 z"; } .toolbar-button:openvertically .arrow { -fx-padding: 0.166667em 0.333333em 0.166667em 0.333333em; /* 2 4 2 4 */ -fx-shape: "M 0 0 h 7 l -3.5 4 z"; } 

Which even allows me to change the color of the back arrow to black on hover.

+8
source

All Articles