I want to change the text color of the Menu control in JavaFX. Currently, the background color of the whole Menu Bar is set to white and the default text color for displaying Menu-s is also white, so I cannot see the actual control, therefore I want to set the text color of the Menu ("File") to black. How do I do that?
Here's the FXML portion:
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
<children>
<MenuBar id="modBar" layoutX="176.0" layoutY="122.0" styleClass="modBar">
<menus>
<Menu id="modItem" mnemonicParsing="false" styleClass="modItem" text="File" />
</menus>
<stylesheets>
<URL value="test.css" />
</stylesheets>
</MenuBar>
</children>
</AnchorPane>
Here's the CSS part:
.modBar
{
-fx-background-color: white;
}
.modItem
{
-fx-color: black;
}
This doesn't work ("File" still remains white). What am I doing wrong? Also, another thing is that I cannot seem to apply anything with CSS to .modItem - it sort-of works in Scene Builder, but disappears once previewed (also the "Stylesheets" selector is missing on all Menu-s in SB).
If you are designing your Javafx application using SceneBuilder then use -fx-text-fill (if not available as option then write it in style input box) as style and give the color you want,it will change the text color of your Textfield .
You can use the fx-background-color css property to set the menu bar background color.
Use the setStyle() Method to Color Texts Label in Java In the line lbl. setStyle("-fx-text-fill: red; -fx-background-color: yellow"); , we applied some additional CSS properties to the label by using setStyle() method.
OK, think I have found the answer. What I did was extract caspian.css out of jfxrt.jar (the default CSS theme JavaFX uses) and inspect everything related to Menu-s:
.menu .label
{
-fx-text-fill: black;
}
This will influence all Menu controls.
By the way, there was a particular build of Scene Builder that might come of interest - b42, this had an additional CSS menu that exposed internal styles of controls/elements, so customizing turns into a straightforward operation (without the need of prior manual extraction of the applied style).
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