Introduction

In this article, we discuss how to create a checkbox in Java.

What is checkbox?

Simply called a selection box, a checkbox is a type of square box where the user can click to select their choice. Using a checkbox he can select multiple values at a time.
The following figure shows some sample checkboxes:
checkbox.jpg
Example
In this example; we create four checkboxes to select the colors of our choice. Now for creating checkboxes we use AWT (Abstract Window Toolkit) and Swing components.
Procedure to follow
Step 1: We create four checkboxes to provide the user with a choice of selecting multiple choices.
  1. JPanel overallGUI = new JPanel();
  2. JPanel chkBxPanl = new JPanel();
  3. chkBxPanl.setLayout(new BoxLayout(chkBxPanl, BoxLayout.PAGE_AXIS));
  4. chkBxPanl.add(Box.createRigidArea(new Dimension(15,5)));
  5. redbtn = new JCheckBox("Red color");
  6. redbtn.setSelected(true);
  7. chkBxPanl.add(redbtn);
  8. chkBxPanl.add(Box.createHorizontalGlue());
  9. bluebtn = new JCheckBox("Blue color");
  10. chkBxPanl.add(bluebtn);
  11. chkBxPanl.add(Box.createHorizontalGlue());
  12. greenbtn = new JCheckBox("Green color");
  13. chkBxPanl.add(greenbtn);
  14. chkBxPanl.add(Box.createHorizontalGlue());
  15. yellowbtn = new JCheckBox("Yellow color");
  16. chkBxPanl.add(yellowbtn);
  17. chkBxPanl.add(Box.createRigidArea(new Dimension(15, 5)));
Step 2: Now we create a simple Panel to display all the four checkboxes we created.
  1. JPanel bxPanl = new JPanel(new GridLayout(3, 3, 23, 23));
  2. JPanel redBx = squrPanl(Color.red, 55);
  3. JPanel blBx = squrPanl(Color.blue, 55);
  4. JPanel grnBx = squrPanl(Color.green, 55);
  5. JPanel yelwBx = squrPanl(Color.yellow, 55);
Step 3: Now we hide all the boxes except the red button.
  1. grnBx.setVisible(false);
  2. blBx.setVisible(false);
  3. yelwBx.setVisible(false);
  4. bxPanl.add(redBx);
  5. bxPanl.add(blBx);
  6. bxPanl.add(grnBx);
  7. bxPanl.add(yelwBx);
  8. overallGUI.add(chkBxPanl);
  9. overallGUI.add(bxPanl);
  10. overallGUI.setOpaque(true);
  11. return overallGUI;
Step 4: Now we create a square panel and set their bounds.
  1. private JPanel squrPanl(Color color, int size)
  2. {
  3. JPanel temporaryPanl = new JPanel();
  4. temporaryPanl.setBackground(color);
  5. temporaryPanl.setMinimumSize(new Dimension(size, size));
  6. temporaryPanl.setMaximumSize(new Dimension(size, size));
  7. temporaryPanl.setPreferredSize(new Dimension(size, size));
  8. return temporaryPanl;
  9. }
  10. private static void showGUI()
  11. {
  12. JFrame.setDefaultLookAndFeelDecorated(true);
  13. JFrame frm = new JFrame("CheckBox");
  14. SwingCheckBxEx showdemo = new SwingCheckBxEx();
  15. frm.setContentPane(showdemo.shwCreatPnl());
  16. frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17. frm.pack();
  18. frm.setVisible(true);
  19. }
  20. public static void main(String[] args)
  21. {
  22. SwingUtilities.invokeLater(new Runnable()
  23. {
  24. public void run()
  25. {
  26. showGUI();
  27. }
  28. });
  29. }
Complete code
SwingCheckBxEx.java
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.Color;
  4. public class SwingCheckBxEx
  5. {
  6. JCheckBox redbtn, bluebtn, greenbtn, yellowbtn;
  7. public JPanel shwCreatPnl()
  8. {
  9. JPanel overallGUI = new JPanel();
  10. JPanel chkBxPanl = new JPanel();
  11. chkBxPanl.setLayout(new BoxLayout(chkBxPanl, BoxLayout.PAGE_AXIS));
  12. chkBxPanl.add(Box.createRigidArea(new Dimension(15, 5)));
  13. redbtn = new JCheckBox("Red color");
  14. redbtn.setSelected(true);
  15. chkBxPanl.add(redbtn);
  16. chkBxPanl.add(Box.createHorizontalGlue());
  17. bluebtn = new JCheckBox("Blue color");
  18. chkBxPanl.add(bluebtn);
  19. chkBxPanl.add(Box.createHorizontalGlue());
  20. greenbtn = new JCheckBox("Green color");
  21. chkBxPanl.add(greenbtn);
  22. chkBxPanl.add(Box.createHorizontalGlue());
  23. yellowbtn = new JCheckBox("Yellow color");
  24. chkBxPanl.add(yellowbtn);
  25. chkBxPanl.add(Box.createRigidArea(new Dimension(15, 5)));
  26. JPanel bxPanl = new JPanel(new GridLayout(3, 3, 23, 23));
  27. JPanel redBx = squrPanl(Color.red, 55);
  28. JPanel blBx = squrPanl(Color.blue, 55);
  29. JPanel grnBx = squrPanl(Color.green, 55);
  30. JPanel yelwBx = squrPanl(Color.yellow, 55);
  31. grnBx.setVisible(false);
  32. blBx.setVisible(false);
  33. yelwBx.setVisible(false);
  34. bxPanl.add(redBx);
  35. bxPanl.add(blBx);
  36. bxPanl.add(grnBx);
  37. bxPanl.add(yelwBx);
  38. overallGUI.add(chkBxPanl);
  39. overallGUI.add(bxPanl);
  40. overallGUI.setOpaque(true);
  41. return overallGUI;
  42. }
  43. private JPanel squrPanl(Color color, int size)
  44. {
  45. JPanel temporaryPanl = new JPanel();
  46. temporaryPanl.setBackground(color);
  47. temporaryPanl.setMinimumSize(new Dimension(size, size));
  48. temporaryPanl.setMaximumSize(new Dimension(size, size));
  49. temporaryPanl.setPreferredSize(new Dimension(size, size));
  50. return temporaryPanl;
  51. }
  52. private static void showGUI()
  53. {
  54. JFrame.setDefaultLookAndFeelDecorated(true);
  55. JFrame frm = new JFrame("CheckBox");
  56. SwingCheckBxEx showdemo = new SwingCheckBxEx();
  57. frm.setContentPane(showdemo.shwCreatPnl());
  58. frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59. frm.pack();
  60. frm.setVisible(true);
  61. }
  62. public static void main(String[] args)
  63. {
  64. SwingUtilities.invokeLater(new Runnable()
  65. {
  66. public void run()
  67. {
  68. showGUI();
  69. }
  70. });
  71. }
  72. }
Output
Fig-1.jpg
fig-2.jpg