MenuItem is intended to be used in conjunction with Menu to provide
options to users. MenuItem serves as the base class for the bulk of JavaFX menus
API.
It has a display text property, as well as an optional graphic node
that can be set on it.
The accelerator property enables accessing the
associated action in one keystroke. Also, as with the Button control,
by using the setOnAction method, you can have an instance of MenuItem
perform any action you wish.
Note: Whilst any size of graphic can be inserted into a MenuItem, the most commonly used size in most applications is 16x16 pixels. This is the recommended graphic dimension to use if you're using the default style provided by JavaFX.
To create a MenuItem is simple:
MenuItem menuItem = new MenuItem("Open");
menuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println("Opening Database Connection...");
}
});
menuItem.setGraphic(new ImageView(new Image("flower.png")));
Refer to the Menu page to learn how to insert MenuItem into a menu
instance. Briefly however, you can insert the MenuItem from the previous
example into a Menu as such:
final Menu menu = new Menu("File");
menu.getItems().add(menuItem);
implements
Menu