JavaFX | CheckMenuItem with examples

CheckMenuItem is a part of the JavaFX library. CheckMenuItem can be added to a menu and it has two states selected and unselected. The user can toggle the menuitems between this two states. CheckMenuItem inherits from the MenuItem class.
Constructors of the class are:
- CheckMenuItem(String t): creates a checkmenuitem with specified text
- CheckMenuItem(String t, Node g):creates a checkmenuitem with specified text and graphic
Commonly used methods:
| method | explanation |
|---|---|
| isSelected() | returns whether the menuitem is selected or not |
| selectedProperty() | Represents the current state of this CheckMenuItem |
| setSelected(boolean v) | sets the value of the property selected |
Below programs illustrate the CheckMenuItem class of JavaFX:
- Java program to create a menu bar and add a menu to it and also add checkmenuitems to menu: This program creates a menubar indicated by the name menu_bar. A menu will be created by name menu and 3 checkmenuitems menuitem1, menuitem2, menuitem3 will be added to the menu and the menu will be added to the menubar menu_bar. The menubar will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a VBox is created, on which addChildren() method is called to attach the menubar inside the scene. Finally, the show() method is called to display the final results.
// Java program to create a menu bar and add// menu to it and also add checkmenuitems to menuimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.control.Alert.AlertType;importjava.time.LocalDate;publicclasscheckmenuitems_0extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Â// set title for the stage       Âstage.setTitle("creating check menu items");       Â// create a menu       ÂMenu menu =newMenu("Menu");       Â// create menuitems       ÂCheckMenuItem menuitem1 =newCheckMenuItem("menu item 1");       ÂCheckMenuItem menuitem2 =newCheckMenuItem("menu item 2");       ÂCheckMenuItem menuitem3 =newCheckMenuItem("menu item 3");       Â// add menu items to menu       Âmenu.getItems().add(menuitem1);       Âmenu.getItems().add(menuitem2);       Âmenu.getItems().add(menuitem3);       Â// create a menubar       ÂMenuBar menu_bar =newMenuBar();       Â// add menu to menubar       Âmenu_bar.getMenus().add(menu);       Â// create a VBox       ÂVBox vbox =newVBox(menu_bar);       Â// create a scene       ÂScene scene =newScene(vbox,500,300);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
- Java program to create a menu bar and add menu to it and also add checkmenuitems to menu and also add an event handler to handle the events: This program creates a menubar indicated by the name menu_bar. A menu will be created by name menu and 3 checkmenuitems menuitem1, menuitem2, menuitem3 will be added to the menu and the menu will be added to the menubar menu_bar. The menubar will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a VBox is created, on which addChildren() method is called to attach the menubar inside the scene. Finally, the show() method is called to display the final results. A label will also be created that will show which checkmenuitem is selected. An action event will be created to process the action when the check menu item is clicked by the user.
// Java program to create a menu bar and add// menu to it and also add checkmenuitems to menu// and also add event handler to handle the eventsimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.control.Alert.AlertType;importjava.time.LocalDate;publicclasscheckmenuitems_2extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Â// set title for the stage       Âstage.setTitle("creating check menu items");       Â// create a menu       ÂMenu menu =newMenu("Menu");       Â// create menuitems       ÂCheckMenuItem menuitem1 =newCheckMenuItem("menu item 1");       ÂCheckMenuItem menuitem2 =newCheckMenuItem("menu item 2");       ÂCheckMenuItem menuitem3 =newCheckMenuItem("menu item 3");       Â// add menu items to menu       Âmenu.getItems().add(menuitem1);       Âmenu.getItems().add(menuitem2);       Âmenu.getItems().add(menuitem3);       Â// label to display events       ÂLabel description =newLabel("\t\t\t\t"                                     Â+"no menu item selected");       Â// create events for menu items       Â// action event       ÂEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {           Âpublicvoidhandle(ActionEvent e)           Â{               Âif(((CheckMenuItem)e.getSource()).isSelected())                   Âdescription.setText                    Â("\t\t\t\t"+ ((CheckMenuItem)e.getSource())                      Â.getText() +" selected");               Âelse                   Âdescription.setText                    Â("\t\t\t\t"+ ((CheckMenuItem)e.getSource())                      Â.getText() +" deselected");           Â}       Â};       Â// add event       Âmenuitem1.setOnAction(event);       Âmenuitem2.setOnAction(event);       Âmenuitem3.setOnAction(event);       Â// create a menubar       ÂMenuBar menu_bar =newMenuBar();       Â// add menu to menubar       Âmenu_bar.getMenus().add(menu);       Â// create a VBox       ÂVBox vbox =newVBox(menu_bar, description);       Â// create a scene       ÂScene scene =newScene(vbox,500,300);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
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/CheckMenuItem.html



