Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JCheckBoxMenuItem & Checked

1. Introduction to JCheckBoxMenuItem

Java Swing provides JCheckBoxMenuItem which is a special menu item derived from JMenuItem. Like normal menu item, a user can pick a Checkbox menu item. But this JCheckBoxMenuItem toggle between selected and non-selected state. It shows a check mark when it is in selected state and when the user clicks on it again; it goes to un-selected state, removing the tick mark in it.

2. About JCheckBoxMenuItem Example

Have a look at the example below:

Java Swing JCheckBox MenuItem Example
Java Swing JCheckBox MenuItem Example

The example has a menu called fruits. When the user clicks the Fruits menu, they can see four Checkbox menu items along with Image Icons. At the bottom of the screen, there is a text field which reports all the checked check boxes. In the above picture, Banana & Cherry check boxes are checked and hence the text field shows Banana and Cherry in it. When the user clicks a menu item, a check mark appears in the check box, and it disappears when the user clicks it again.

3. Create Fruits Menu

In the JFrame’s constructor, we create a JMenuBar and attach it to the JFrame. Later, we create a JMenu with title Fruits and add it to the menu bar. The code is the code:

4. Create JCheckBoxMenuItem with Icon

Java Swing’s ImageIcon class represents an image as an object. In the constructor, we pass the path to the image icon. You can create 24×24 pixel image to denote a fruit and place it in the C:\Temp folder or change the below code to refer the image location.

Once ImageIcon objects are ready, we can create JCheckBoxmenuItem via 2 param constructor. The first param is the title for the JCheckboxMenuItem which will get displayed along with the CheckBox. Second param specifies the image icon for the checkbox menu item.

5. Add JTextField to Frame’s Bottom

Our JFrame class has a data member JTextField which we will use to report the selected check box menu.

The below code snippet creates the JTextField and adds it to the bottom of the JFrame Window. The JFrame is managed by the Border Layout Manager (Refer Code Snippet 01).

6. Get Checked State of JCheckBoxMenuItem

JCheckBoxMenuItem will raise ActionEvent when the user clicks it. Below code handles the click event of the JCheckBoxMenuItem:

In the above code, we use the getState method to know the state of the JCheckBoxMenuItem. When the method returns true, the check box menu item is in checked state. One can also use the isSelected method, which returns the same result. Above Click Handler adds the menu item name to the text box when the item is in selected state and it removes the menu item name when the item is un-selected state.

7. Code Implementation YouTube – Demo

JCheckBoxMenuItem & Checked State : Demo

8. Code Reference



Categories: Swing

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.