JavaFX | CheckMenuItem with examples Last Updated : 23 Oct, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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 // 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 Comment More infoAdvertise with us Next Article JavaFX | CheckMenuItem with examples A andrew1234 Follow Improve Article Tags : Java JavaFX Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to 12 min read Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android 4 min read Like