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:

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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class SwingCheckedMenuItems extends JFrame { public SwingCheckedMenuItems(String title) { //Sample 01: Set Size and Position super(title); setBounds(100, 100, 450, 250); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 02: Create Menu Bar and add to JFrame JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); //Sample 03: Create Menu and Add it to Menu bar JMenu mnuFruits = new JMenu("Fruits"); menubar.add(mnuFruits); |
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.
1 2 3 4 5 |
//Sample 04: Create Image Icons ImageIcon icoApple = new ImageIcon("C:\\temp\\Apple.png"); ImageIcon icoBanana = new ImageIcon("C:\\temp\\Banana.png"); ImageIcon icoCherry = new ImageIcon("C:\\temp\\Cherry.png"); ImageIcon icoGrapes = new ImageIcon("C:\\temp\\Grapes.png"); |
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.
1 2 3 4 5 6 7 8 9 |
//Sample 05: Create CheckBox Menu Items JCheckBoxMenuItem cmiApple = new JCheckBoxMenuItem("Apple", icoApple); JCheckBoxMenuItem cmiBanana = new JCheckBoxMenuItem("Banana", icoBanana); JCheckBoxMenuItem cmiCherry = new JCheckBoxMenuItem("Cherry", icoCherry); JCheckBoxMenuItem cmiGrapes = new JCheckBoxMenuItem("Grapes", icoGrapes); |
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.
1 2 3 4 5 |
@SuppressWarnings("serial") public class SwingCheckedMenuItems extends JFrame { //Sample 07: Declare JTextField JTextField jtf; |
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).
1 2 3 |
//Sample 08: Create Text Field and to Top jtf = new JTextField(); add(jtf, BorderLayout.SOUTH); |
6. Get Checked State of JCheckBoxMenuItem
JCheckBoxMenuItem will raise ActionEvent when the user clicks it. Below code handles the click event of the JCheckBoxMenuItem:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Sample 09: Set Action Listeners cmiApple.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str = jtf.getText(); if(cmiApple.getState() == true) str = str.concat("[Apple]"); else str = str.replace("[Apple]", ""); jtf.setText(str); } }); |
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
8. Code Reference
8.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { SwingCheckedMenuItems frame = new SwingCheckedMenuItems("Swing Checkbox Menu Items"); frame.setVisible(true); } } |
8.2 SwingCheckedMenuItems.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
package tube.coding.examples; import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JTextField; @SuppressWarnings("serial") public class SwingCheckedMenuItems extends JFrame { //Sample 07: Declare JTextField JTextField jtf; public SwingCheckedMenuItems(String title) { //Sample 01: Set Size and Position super(title); setBounds(100, 100, 450, 250); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 02: Create Menu Bar and add to JFrame JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); //Sample 03: Create Menu and Add it to Menu bar JMenu mnuFruits = new JMenu("Fruits"); menubar.add(mnuFruits); //Sample 04: Create Image Icons ImageIcon icoApple = new ImageIcon("C:\\temp\\Apple.png"); ImageIcon icoBanana = new ImageIcon("C:\\temp\\Banana.png"); ImageIcon icoCherry = new ImageIcon("C:\\temp\\Cherry.png"); ImageIcon icoGrapes = new ImageIcon("C:\\temp\\Grapes.png"); //Sample 05: Create CheckBox Menu Items JCheckBoxMenuItem cmiApple = new JCheckBoxMenuItem("Apple", icoApple); JCheckBoxMenuItem cmiBanana = new JCheckBoxMenuItem("Banana", icoBanana); JCheckBoxMenuItem cmiCherry = new JCheckBoxMenuItem("Cherry", icoCherry); JCheckBoxMenuItem cmiGrapes = new JCheckBoxMenuItem("Grapes", icoGrapes); //Sample 06: Add Menu Items to Menu mnuFruits.add(cmiApple); mnuFruits.add(cmiBanana); mnuFruits.add(cmiCherry); mnuFruits.add(cmiGrapes); //Sample 08: Create Text Field and to Top jtf = new JTextField(); add(jtf, BorderLayout.SOUTH); //Sample 09: Set Action Listeners cmiApple.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str = jtf.getText(); if(cmiApple.isSelected() == true) str = str.concat("[Apple]"); else str = str.replace("[Apple]", ""); jtf.setText(str); } }); cmiBanana.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str = jtf.getText(); if(cmiBanana.getState() == true) str = str.concat("[Banana]"); else str = str.replace("[Banana]", ""); jtf.setText(str); } }); cmiCherry.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str = jtf.getText(); if(cmiCherry.getState() == true) str = str.concat("[Cherry]"); else str = str.replace("[Cherry]", ""); jtf.setText(str); } }); cmiGrapes.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str = jtf.getText(); if(cmiGrapes.isSelected() == true) str = str.concat("[Grapes]"); else str = str.replace("[Grapes]", ""); jtf.setText(str); } }); } } |
Categories: Swing
Tags: getState, ImageIcon, isSelected, JCheckBoxMenuItem, JMenu