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 menu
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.*;
import
javafx.stage.Stage;
import
javafx.scene.control.Alert.AlertType;
import
java.time.LocalDate;
public
class
checkmenuitems_0
extends
Application {
// launch the application
public
void
start(Stage stage)
{
// set title for the stage
stage.setTitle(
"creating check menu items"
);
// create a menu
Menu menu =
new
Menu(
"Menu"
);
// create menuitems
CheckMenuItem menuitem1 =
new
CheckMenuItem(
"menu item 1"
);
CheckMenuItem menuitem2 =
new
CheckMenuItem(
"menu item 2"
);
CheckMenuItem menuitem3 =
new
CheckMenuItem(
"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 =
new
MenuBar();
// add menu to menubar
menu_bar.getMenus().add(menu);
// create a VBox
VBox vbox =
new
VBox(menu_bar);
// create a scene
Scene scene =
new
Scene(vbox,
500
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
public
static
void
main(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 events
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.*;
import
javafx.stage.Stage;
import
javafx.scene.control.Alert.AlertType;
import
java.time.LocalDate;
public
class
checkmenuitems_2
extends
Application {
// launch the application
public
void
start(Stage stage)
{
// set title for the stage
stage.setTitle(
"creating check menu items"
);
// create a menu
Menu menu =
new
Menu(
"Menu"
);
// create menuitems
CheckMenuItem menuitem1 =
new
CheckMenuItem(
"menu item 1"
);
CheckMenuItem menuitem2 =
new
CheckMenuItem(
"menu item 2"
);
CheckMenuItem menuitem3 =
new
CheckMenuItem(
"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 =
new
Label(
"\t\t\t\t"
+
"no menu item selected"
);
// create events for menu items
// action event
EventHandler<ActionEvent> event =
new
EventHandler<ActionEvent>() {
public
void
handle(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 =
new
MenuBar();
// add menu to menubar
menu_bar.getMenus().add(menu);
// create a VBox
VBox vbox =
new
VBox(menu_bar, description);
// create a scene
Scene scene =
new
Scene(vbox,
500
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
public
static
void
main(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