Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing text color of a Menu control in JavaFX with FXML/CSS

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).

like image 947
XXL Avatar asked Jun 15 '12 13:06

XXL


People also ask

How do you change the text color in a JavaFX text field?

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 .

How do I change the color of the menu bar in JavaFX?

You can use the fx-background-color css property to set the menu bar background color.

How do I add color to a label in JavaFX?

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.


1 Answers

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).

like image 82
XXL Avatar answered Oct 15 '22 10:10

XXL