JCheckBox Component in Java

This is a tutorial in which we will going to create a program that has the JCheckBox Component using Java. The JCheckBox is used to let the user select one or more options. It is also a component that an item can be selected or deselected, and which displays its state to the user. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of jCheckBoxComponent.java. 2. Import the following packages:
  1. import java.awt.*; //used to access the FlowLayout class
  2. import java.awt.event.*; //used to access the ItemListener and ItemEvent class
  3. import javax.swing.*; //used to access the JFrame,JLabel, and JCheckBox class
3. Initialize your variable in your Main, variable frame for creating JFrame, and variable letter a to f for the checkboxes.
  1.     JFrame frame = new JFrame("JCheckBox Component");
  2.    
  3.     JCheckBox a = new JCheckBox("Bird");
  4.     JCheckBox b = new JCheckBox("Chicken");
  5.     final JCheckBox c = new JCheckBox("Pig");
  6.     JCheckBox d = new JCheckBox("Cow");
  7.     JCheckBox e = new JCheckBox("Dog");
  8.     JCheckBox f = new JCheckBox("Cat");
As you can see the above code, I created 6 variables for the JCheckBox component. Variable a for Bird, variable b for Chicken, variable c for Pig, variable d for Cow, variable e for Dog, and variable f for Cat. I used the final keyword for variable c as checkbox because I want to create an inner class within it from the ItemEvent and ItemListener class. 4. Add ItemListener to JCheckBox variable c with the itemStateChanged method. This will trigger to check if the checkbox is checked or not using the isSelected method. Have this code below:
  1.     c.addItemListener(new ItemListener() {
  2.       public void itemStateChanged(ItemEvent e) {
  3.         System.out.println("Pig is checked? " + c.isSelected());
  4.       }
  5.       });
5. Now, to select the item to checkbox after running it, use the setSelected method and return to true.
  1. a.setSelected(true);
6. Add a JLabel and a JCheckBox component to the frame using the add method, then pack the frame so that the component/control will be seen directly to the frame. Also set its visibility to true and have it the close operation to exit.
  1.     frame.getContentPane().add(new JLabel("animals"));
  2.     frame.getContentPane().add(a);
  3.     frame.getContentPane().add(b);
  4.     frame.getContentPane().add(c);
  5.     frame.getContentPane().add(d);
  6.     frame.getContentPane().add(e);
  7.     frame.getContentPane().add(f);
  8.     frame.getContentPane().setLayout(new FlowLayout());
  9.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10.     frame.pack();
  11.     frame.setVisible(true);
Output: output Here's the full code of this tutorial:
  1. import java.awt.*; //used to access the FlowLayout class
  2. import java.awt.event.*; //used to access the ItemListener and ItemEvent class
  3. import javax.swing.*; //used to access the JFrame,JLabel, and JCheckBox class
  4. public class jCheckBoxComponent {
  5.  
  6.   public static void main(String[] args) {
  7.  
  8.     JFrame frame = new JFrame("JCheckBox Component");
  9.    
  10.     JCheckBox a = new JCheckBox("Bird");
  11.     JCheckBox b = new JCheckBox("Chicken");
  12.     final JCheckBox c = new JCheckBox("Pig");
  13.     JCheckBox d = new JCheckBox("Cow");
  14.     JCheckBox e = new JCheckBox("Dog");
  15.     JCheckBox f = new JCheckBox("Cat");
  16.    
  17.     c.addItemListener(new ItemListener() {
  18.       public void itemStateChanged(ItemEvent e) {
  19.         System.out.println("Pig is checked? " + c.isSelected());
  20.       }
  21.       });
  22.      
  23.     a.setSelected(true);
  24.    
  25.     frame.getContentPane().add(new JLabel("animals"));
  26.     frame.getContentPane().add(a);
  27.     frame.getContentPane().add(b);
  28.     frame.getContentPane().add(c);
  29.     frame.getContentPane().add(d);
  30.     frame.getContentPane().add(e);
  31.     frame.getContentPane().add(f);
  32.     frame.getContentPane().setLayout(new FlowLayout());
  33.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.     frame.pack();
  35.     frame.setVisible(true);
  36.   }
  37. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment