How to Create and Add Functionality to a Check Menu Item in JavaFX 


               


How to Create and Add Functionality to a Check Menu Item in JavaFX

Java


In this article, we show how to create and add functionality to a check menu item in JavaFX.

A check menu item is a menu item in a menu that can be checked or unchecked out.

More than likely, you've seen these in commercial software.

Check menu items are used most of the time to set a setting in the software, such as, "Show Line Numbers" to display the line numbers on the screen.

So in this video, we'll create a check menu item that displays, "Show Line Numbers". This can be checked on or off. We then show how to handle the logic of this check menu item so that we can add functionality, meaning do something if it's checked and do something if it's unchecked.

So the Java code to create a check menu item and add functionality to the check menu item is shown below.



So the first thing we do is we create the CheckMenuItem. In this example, we name it showlines. We give it a text surface value of "Show Line Numbers".

We then must add this to our menu. Our menu in this case is named settingsmenu.

So up until this point, we have the "Show Line Numbers" menu item added to the Settings Menu. However, there is no functionality added to this menu item. So now we add functionality so that it does something based on it being checked or unchecked.

So to do this, we add event handling to the menu item.

We take the menu item, showlines, and use the setOnAction() function to have event handling. If the user clicks on the menu item, we can tell whether it is clicked or not through the isSelected() function. If the item is checked, the isSelected() function will return true. If unchecked, the isSelected() function will return false. You can then make it execute whatever code you want based on this return boolean value.

The code above is just to show you how to create the check menu item and add functionality but it actually doesn't execute anything.

Below we have the full Java code that executes to the screen whether the menu item is checked or unchecked if the check menu item is selected or not. It doesn't actually add the line numbers to the screen. That would make the code more burdensome and complex. It simply states whether the check menu item is selected or not.





So the first thing we do, like in all JavaFX applications, is we import all the packages that are necessary for the code.

After this, we create a public class that extends the Application class. Therefore, the class that we make inherits all the functionality of the Application class.

We then overwrite the start() method, set the stage title to "Software Program", create a menu bar named menubar, create a file menu, add all the menu items to the file menu, do the same for the edit menu, and then create the settings menu.

We then create a check menu item named showlines and set it equal to a text of "Show Line Numbers".

We add this item to the Settings menu.

We create a label named labelresponse, so that we can add this label to the window that outputs whether the check menu item is checked or not.

To add functionality to this check menu item, we take the check menu item, showlines, and use the setOnAction() function to add event handling to this menu item.

We check if the check menu item is selected by using the isSelectded() function. If the showlines CheckMenuItem is selected, we output on the label, "The Show Line Numbers setting has been enabled". If the showlines CheckMenuItem is not selected, we output on the label, "The Show Line Numbers setting is disabled".

We then add the settingsmenu to the menu bar.

The rest of the code simply creates the layout, sets it to the top of the menu bar, and sets the label to the layout, creates a scene, and sets the scene to the stage, and shows the stage.

To see how this menu looks with the check menu item, see the image at the following link: JavaFX check menu item.

So this is just a basic program to show how to add a check menu item to a JavaFX menu and add functionality to it.


Related Resources

How to Retrieve Data from a Text Field in JavaFX

How to Retrieve Data from a Radio Button Group in JavaFX

How to Retrieve Data from a Group of Check Boxes in JavaFX

How to Retrieve Data from a ChoiceBox in JavaFX

How to Retrieve Data from a ListView

How to Check if a Text Field is Empty in JavaFX

How to Select an Item By Default in JavaFX

How to Disable a Button After It's Clicked in JavaFX




HTML Comment Box is loading comments...