I have a small panel that has 3 JComboBox
and 2 JCheckBox
which belongs to a JButtonGroup
. I am trying to check if the panel form is valid and that all required fields doesn't have a null selection so I created a method.
public static boolean isValid(List<Component> aComponent){
boolean isValid = false ;
for(Component c: aComponent){
if(c instanceof JComboBox){
isValid = (((JComboBox) c).getSelectedIndex() > -1);
JOptionPane.showMessageDialog(null,"ComboBox returned: "+isValid);
}else if(c instanceof JCheckBox){
isValid = ( ((JCheckBox) c).isSelected() );
JOptionPane.showMessageDialog(null,"Checkbox returned: "+isValid);
}else if(c instanceof JTextField){
isValid = ((JTextField) c).getText().isEmpty();
JOptionPane.showMessageDialog(null,"TextField returned: "+isValid);
}
}
return isValid;
}
The problem is, I only need to have one selected JCheckbox
for Student Type.
I need to be able to check if the Button Group has at least 1 JChecBox
checked.
The following block will return false since both Transferee and New are instances of JCheckBox. But I only need 1 JCheckBox
checked to make the form valid.
How do I check if at least 1 is checked from the button group using my isValid()
method?