Chapter: Java The Complete Reference - Introducing GUI Programming with JavaFX - Introducing JavaFX Menus

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Add Images to Menu Items - JavaFX

You can add images to menu items or use images instead of text. The easiest way to add an image is to specify it when the menu item is being constructed using this constructor:

Add Images to Menu Items

 

You can add images to menu items or use images instead of text. The easiest way to add an image is to specify it when the menu item is being constructed using this constructor:

 

MenuItem(String name, Node image)

 

It creates a menu item with the name specified by name and the image specified by image. For example, here the About menu item is associated with an image when it is created.

 

ImageView aboutIV = new ImageView("aboutIcon.gif");

 

MenuItem about = new MenuItem("About", aboutIV);

 

After this addition, the image specified by aboutIV will be displayed next to the text “About” when the Help menu is displayed, as shown here:


One last point: You can also add an image to a menu item after the item has been created by calling setGraphic( ). This lets you change the image during program execution.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.