2

I would like to change the MenuItem's text to bold when the user clicks on it. To manage to do this I'm using setStyle, but unfortunately it has no visible effect. The css makes no effect when the user selects the MenuItem option. The css styling only has an effect in initialize method.

I created a minimal reproducible example for the problem:

MAIN:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public final class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("MainView.fxml"));

        Scene scene = new Scene(root, 850.0, 650.0);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

MAIN FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane  prefHeight="475.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="MainController">
    <top>
      <MenuBar BorderPane.alignment="CENTER">
        <menus>
          <Menu fx:id="menu" mnemonicParsing="false" text="Switch Chart">
            <items>
              <MenuItem text="MenuItem1" />
              <MenuItem onAction="#setFontBold" text="MenuItem2" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
</BorderPane>

MAIN CONTROLLER:

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;

public class MainController implements Initializable {
    @FXML private Menu menu;

    private List<MenuItem> menuItems;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        menuItems = menu.getItems();
        menuItems.get(0).setStyle("-fx-font-weight: bold");
    }

    public void setFontBold() {
        menuItems.get(1).setStyle("-fx-font-weight: bold");
        System.out.println(menuItems.get(1).getStyle());
        System.out.println("Font set to bold in menuitem with index 1 has no effect...");
    }
}

So I get the following css effects, even after selecting the first indexed menu item (so onAction=#setFontBold not changes anything visually).

No bold effect. - only if you set the style in initialize()

I'm curious about why it makes no difference? It definitely sets the style to bold, but it is displayed as a regular font even after changing it to bold.

EDIT: JavaFX version: 8.0.211-b10

Ricky
  • 553
  • 6
  • 21
  • worksforme .. check if this is really the code you are running: a bit suspicious that it throws a NPE in initialize (because the id of the menu in the fxml incorrectly starts with a capital letter) – kleopatra Aug 25 '19 at 10:43
  • Yes, you are right. I forgot to edit that out. Now it's fine. Even though, the MenuItem with the text MenuItem2 stays in regular font for me (even after I click on it). It really works for you? Then why don't for me? @kleopatra – Ricky Aug 25 '19 at 11:29
  • no idea - which version do you have? mine is fx11 - hach missed your edit: yeah, than it might be a bug that's fixed meanwhile .. didn't check, though. Most probably it's a missing listener in the skin bowels of ContextMenu/Container: need one to update the style of the control if they are changed on the MenuItem (which is _not_ a control) But again, didn't check :) – kleopatra Aug 25 '19 at 11:32
  • yeah, the MenuItemContainer (parent for a single MenuLabel) sets the style initially only (fx) without any effort to keep it sync'd, in later version (fx11) it binds the style property to the item's style – kleopatra Aug 25 '19 at 11:46
  • Thank you again for helping me out kleopatra. I planned to upgrade my javafx version anyway. There are some obvious reasons that I should do that. – Ricky Aug 25 '19 at 11:55

1 Answers1

0

If I would have to stick to javafx 8 and switching was the requirement, I would have used RadioMenuItem. It does the job as expected, but does not make MenuItem text bold.

From official docs, I found following excerpt that would be useful.

ToggleGroup toggleGroup = new ToggleGroup();

RadioMenuItem radioItem1 = new RadioMenuItem("Option 1");
radioItem.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
    System.out.println("radio toggled");
}
});
radioItem1.setToggleGroup(toggleGroup);
RadioMenuItem radioItem2 = new RadioMenuItem("Option 2");
radioItem.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
    System.out.println("radio toggled");
}
});

radioItem2.setToggleGroup(toggleGroup);
AbsoluteDev
  • 154
  • 2
  • 9