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 applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating check menu items");// create a menuMenu menu =newMenu("Menu");// create menuitemsCheckMenuItem menuitem1 =newCheckMenuItem("menu item 1");CheckMenuItem menuitem2 =newCheckMenuItem("menu item 2");CheckMenuItem menuitem3 =newCheckMenuItem("menu item 3");// add menu items to menumenu.getItems().add(menuitem1);menu.getItems().add(menuitem2);menu.getItems().add(menuitem3);// create a menubarMenuBar menu_bar =newMenuBar();// add menu to menubarmenu_bar.getMenus().add(menu);// create a VBoxVBox vbox =newVBox(menu_bar);// create a sceneScene scene =newScene(vbox,500,300);// set the scenestage.setScene(scene);stage.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(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 applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating check menu items");// create a menuMenu menu =newMenu("Menu");// create menuitemsCheckMenuItem menuitem1 =newCheckMenuItem("menu item 1");CheckMenuItem menuitem2 =newCheckMenuItem("menu item 2");CheckMenuItem menuitem3 =newCheckMenuItem("menu item 3");// add menu items to menumenu.getItems().add(menuitem1);menu.getItems().add(menuitem2);menu.getItems().add(menuitem3);// label to display eventsLabel description =newLabel("\t\t\t\t"+"no menu item selected");// create events for menu items// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){if(((CheckMenuItem)e.getSource()).isSelected())description.setText("\t\t\t\t"+ ((CheckMenuItem)e.getSource()).getText() +" selected");elsedescription.setText("\t\t\t\t"+ ((CheckMenuItem)e.getSource()).getText() +" deselected");}};// add eventmenuitem1.setOnAction(event);menuitem2.setOnAction(event);menuitem3.setOnAction(event);// create a menubarMenuBar menu_bar =newMenuBar();// add menu to menubarmenu_bar.getMenus().add(menu);// create a VBoxVBox vbox =newVBox(menu_bar, description);// create a sceneScene scene =newScene(vbox,500,300);// set the scenestage.setScene(scene);stage.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(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
				
					



