JAVA - How To Uncheck And Check All JCheckBox In Java Netbeans
__________________________________________________________________________
Project Source Code:
package javadb_001;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class WorkClass {
public static class frame extends JFrame{
// create a table of JCheckBoxes
JCheckBox[] jch = {new JCheckBox(),new JCheckBox(),
new JCheckBox(),new JCheckBox(),
new JCheckBox(),new JCheckBox(),
new JCheckBox(),new JCheckBox(),
new JCheckBox(),new JCheckBox(),
new JCheckBox(),new JCheckBox(),
new JCheckBox(),new JCheckBox(),
new JCheckBox(),new JCheckBox(),
new JCheckBox(),new JCheckBox(),
new JCheckBox(),new JCheckBox(),
new JCheckBox(),new JCheckBox()};
//button to check and uncheck all checkBoxes
JButton btn = new JButton("check");
public frame(){
//create a loop too add all jcheckboxes
int j = 10;//for change the place of jcheckboxes
for(int i = 0; i < jch.length; i++){
jch[i].setBounds(j, 100, 20, 20);
add(jch[i]);
j += 30;
}
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//create a loop to verify all jcheckboxes
for(int i = 0; i < jch.length; i++){
//if is not checked than make it checked and change the text of the button
if(!jch[i].isSelected()){
jch[i].setSelected(true);
btn.setText("uncheck");
}
//if is it checked than make it unchecked and change the text of the button
else{
jch[i].setSelected(false);
btn.setText("check");
}
}
}
});
btn.setBounds(290, 30, 100, 30);
add(btn);
setLayout(null);
getContentPane().setBackground(Color.decode("#bdb76b"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(680,200);
setVisible(true);
}
}
public static void main(String[] args){
new frame();
}
}
///////////////////// END//////////////OUTPUT:
java check all jcheckbox |
java uncheck all jcheckbox |
Download Projects Source Code