How to use JCheckBox in netbeans..

A javax.swing.JCheckBox shows a small box that is either marked or unmarked. When you click on it, it changes from checked to unchecked or vice versa automatically. You don’t have to do any programming for the checkbox to change.

There are two ways to use a checkbox:

  • Active. Use addActionListeneror addItemListener() so that a method will be called whenever the checkbox is changed.
  • Passive. Use isSelected() to test if a checkbox is checked.

In here I will show how to use it in netbeans. You can easily drag and drop the checkbox from swing palate and modify the captions whatever you want. It is up to you to design it in creatively. See the example code below and get a basic idea about how it behave in a application.

I assume that you have successfully added three check boxes in to application.In my application i am going to do something like when user check the check box below button will enable and it’s text will change. You can use as you wish in your application.

Right click on the jCheckBox and click events —->Action —->Action performed.

You can see below method and you can edit the method body as i did in my code.

private void chkFirstActionPerformed(java.awt.event.ActionEvent evt) {

       if(chkFirst.isSelected()==true) // if check box is checked
            btnOne.setEnabled(true);  // button will enable
            btnOne.setText("Hi im enabled");// button text will change into parameter msg..
 else   {    // if check box unchecked 
            btnOne.setEnabled(false); //button disabled
            btnOne.setText("Oh im disabled");//change the text on the button
        // TODO add your handling code here:

same code goes to other check boxes.

private void chkSecondActionPerformed(java.awt.event.ActionEvent evt) {

            btnsecond.setText("Hi im enabled");
 else   {
            btnsecond.setText("Oh im disabled");
        // TODO add your handling code here:

private void chkThirdActionPerformed(java.awt.event.ActionEvent evt) {

            btnThird.setText("Hi im enabled");
 else   {
            btnThird.setText("Oh im disabled");
        // TODO add your handling code here:

I have used action performed even to access the check box state.If user click on the check box it will follow the immediate body instructions. I used it to enable or disable a JButton in my application. See my source-code for more details and if you have a problem comment on me.

Download the complete project on here 

15 thoughts on “How to use JCheckBox in netbeans..

  1. Great a nice work.

    Can you post a cording how to make a audit file in java.that mean in a java application,when application in working,all the things must be written to a file(audit file).help me

  2. i have errors here– if(chkFirst.isSelected()==true) // if check box is checked–… im using netbeans 7.0 .. my error is chkFirst … do i need to change it ?

  3. i want to get a data from the database using checkbox….can you please tell me the coding….ex if i click the check box1 it have to get the id(particula column)details from the database…

  4. This is great and I found it very helpful. I am having a problem with the isSelected() it is not being recognised. I have added:
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    yet nothing seems to help.
    Am I missing something?

      • Yeah, I have NetBeans IDE 7.1, I have changed the name of the JCheckBox but I don’t think that is the problem. I think I have it working by using isEnabled() instead. I am having problems using append() at the minute, NetBeans isn’t recognising it at all. Do you have any ideas?

  5. I need to show a password in a password field (which is currently in asterisk marks when typed), I need the check box to make my password visible. How can I achieve that???

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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