Credits

Saturday, February 13, 2010

Java's Check Boxes

Java has several GUI components, one of the commonly used components is a check box. This is non - exclusive like a radio button, meaning for several check boxes, they can all be selected. But to make the components exclusive, then they have to be grouped using the ButtonGroup class. 

Check the sample below.



[+/-] show/hide



import javax.swing.*;
import java.awt.*;

public class CheckBoxes extends JFrame {
public CheckBoxes() {
super("CheckBoxes");
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel jobLabel=new Jlabel("Select job position:");
JCheckBox programmer = new JCheckBox("Programmer",true);
JCheckBox analyst = new JCheckBox("Analyst");
JCheckBox sysadmin = new JCheckBox("System Administrator");
JCheckBox dbadmin = new JCheckBox("Database Administrator");
FlowLayout flo = new FlowLayout();
ButtonGroup jobs = new ButtonGroup();
jobs.add(programmer);
jobs.add(analyst);
jobs.add(sysadmin);
setLayout(flo);
add(jobLabel);
add(programmer);
add(analyst);
add(dbadmin);
add(sysadmin);
setVisible(true);
}

public static void main(String[] arguments) {
CheckBoxes app = new CheckBoxes();
}
}

AddThis