Open In App

JavaFX | CustomMenuItem

CustomMenuItem is a part of the JavaFX library. CustomMenuItem allows different types of nodes as its menu item. One of the useful property of the customMenuItem is hideOnClick. This property states whether the menu should be hidden when the user clicks the menuitem or not.
Constructors of CustomMenuItem: 
 

  1. CustomMenuItem(Node n): creates a menuitem with the node specified
  2. CustomMenuItem(Node n, boolean b): creates a menuitem with the node and hide on click property specified

Commonly used methods: 



 

method explanation
getContent() gets the value of the property content
isHideOnClick() gets the value of the property hideOnClick
setHideOnClick(boolean v) sets the value of the property hideOnClick
setContent(Node v) sets the node as the content for the menu item

Below programs will illustrate the use of CustomMenuItem: 
 






// Program to create a custom menu items and add it to the menu
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import javafx.stage.Stage;
import javafx.scene.text.Text.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
 
public class CustomMenuItem_1 extends Application {
 
    // Launch the application
    public void start(Stage stage)
    {
        // Set title for the stage
        stage.setTitle("creating CustomMenuItem ");
 
        // Create a tile pane
        TilePane r = new TilePane();
 
        // Create a label
        Label description_label =
                    new Label("This is a CustomMenuItem example ");
 
        // Create a menu
        Menu menu = new Menu("Menu");
 
        // Create menuitems
        CustomMenuItem menuitem_1 =
                    new CustomMenuItem(new Button("MenuItem 1"));
        CustomMenuItem menuitem_2 =
                    new CustomMenuItem(new Label("MenuItem 2"));
        CustomMenuItem menuitem_3 =
                    new CustomMenuItem(new CheckBox("MenuItem 3"));
 
        // Add menu items to menu
        menu.getItems().add(menuitem_1);
        menu.getItems().add(menuitem_2);
        menu.getItems().add(menuitem_3);
 
        // Create a menubar
        MenuBar menubar = new MenuBar();
 
        // Add menu to menubar
        menubar.getMenus().add(menu);
 
        // Create a VBox
        VBox vbox = new VBox(menubar);
 
        // Create a scene
        Scene scene = new Scene(vbox, 200, 200);
 
        // Set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String args[])
    {
        // Launch the application
        launch(args);
    }
}




// Program to create custom menu items and
// Add it to the menu and use the property hide on click
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import javafx.stage.Stage;
import javafx.scene.text.Text.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
public class CustomMenuItem_2 extends Application {
 
    // Launch the application
    public void start(Stage stage)
    {
        // Set title for the stage
        stage.setTitle("creating CustomMenuItem ");
 
        // Create a tile pane
        TilePane r = new TilePane();
 
        // Create a label
        Label description_label =
                   new Label("This is a CustomMenuItem example ");
 
        // Create a menu
        Menu menu = new Menu("Menu");
 
        // Create menuitems
        CustomMenuItem menuitem_1 =
                   new CustomMenuItem(new Button("MenuItem 1"));
        CustomMenuItem menuitem_2 =
                   new CustomMenuItem(new Slider());
        CustomMenuItem menuitem_3 =
                   new CustomMenuItem(new CheckBox("MenuItem 3"));
        CustomMenuItem menuitem_4 =
                   new CustomMenuItem(new ChoiceBox(FXCollections
                     .observableArrayList("choice 1",
                                   "choice 2", "choice 3")));
 
        // Cet hide on click property
        menuitem_2.setHideOnClick(false);
        menuitem_4.setHideOnClick(false);
        menuitem_1.setHideOnClick(true);
        menuitem_3.setHideOnClick(true);
 
        // Add menu items to menu
        menu.getItems().add(menuitem_1);
        menu.getItems().add(menuitem_2);
        menu.getItems().add(menuitem_3);
        menu.getItems().add(menuitem_4);
 
        // Create a menubar
        MenuBar menubar = new MenuBar();
 
        // Add menu to menubar
        menubar.getMenus().add(menu);
 
        // Create a VBox
        VBox vbox = new VBox(menubar);
 
        // Create a scene
        Scene scene = new Scene(vbox, 200, 200);
 
        // Cet the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String args[])
    {
        // Launch the application
        launch(args);
    }
}

Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javafx/2/api/javafx/scene/control/CustomMenuItem.html
 


Article Tags :