user3697373
user3697373

Reputation: 83

Add icon to menuItem in JavaFx by CSS

I use this CSS to add icon to menuItem on JavaFX application:

    #mniOpen > .label{
    -fx-graphic:url(media/open.png);
}

It works, but one problem: my menuItem has a shortcut key (Ctrl+O), so in this item there are two label. In the result, the icon repeats twice for this menuItem:

enter image description here

How can remove the second icon (for Ctrl+O) ?

Upvotes: 3

Views: 7928

Answers (1)

Keyur Bhanderi
Keyur Bhanderi

Reputation: 1544

Using css

#mniOpen > .label{
    -fx-graphic: url("media/open.png");
}
#mniOpen .accelerator-text{
    -fx-graphic: none;
}

Without using css

  Image openIcon = new Image(getClass().getResourceAsStream("media/open.png"));
  ImageView openView = new ImageView(openIcon);
  openView.setFitWidth(15);
  openView.setFitHeight(15);
  MenuItem newMenuItem = new MenuItem("Open");
  newMenuItem.setGraphic(openView);
  newMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.O, KeyCombination.CONTROL_DOWN));

Upvotes: 10

Related Questions