How to disable JCheckBox if not checked in Java

Java 8Object Oriented ProgrammingProgramming

The following is an example to disable JCheckBox if not checked in Java:

Example

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
public class SwingDemo {
   public static void main(String[] args) {
      JCheckBox checkBox = new JCheckBox("Demo", true);
      checkBox.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            if (checkBox.isEnabled())
               checkBox.setEnabled(false);
            else
               checkBox.setEnabled(true);
         }
      });
      JOptionPane.showMessageDialog(null, checkBox);
   }
}

Output

Now, when you will uncheck the above checkbox, it will get disabled:

raja
Published on 06-May-2019 11:04:41
Advertisements