How to loop the selected check box components
Hi,
I have 41 check boxs, i am adding all these checkboxes toJPanel directly, if i select 30 of them, now my requirement is : how can i know what are all the 30 checkboxs components got selected.
Is there any functionality (like looping) so that we can know all these 30 selected checkboxsnames. Following is my snippet :
checkBox23.setText("OpenOutputCount");
checkBox23.setActionCommand("OpenOutputCount");
panel10.add(checkBox23, new GridBagConstraints(0, 11, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
any snippet is appreciated.
Regards,
Ravi Kumar
# 1
class Whatever implrements ItemListener {
/* ... */
checkBox23.addItemListener(this);
/* ... */
public void itemStateChanged(ItemEvent ie) {
if ( "OpenOutputCount".equals(ie.getItem()) ) {
boolean state = checkBox23..getState();
}
}
Now as to looping ... what you might want to do is - instead of naming each Checkbox, place them in an array:
String[] cbNames = new String[] { "a_anme", "b_name", "OpenOutputCount", " ... ", "z_name" };
CheckBox[] checkBoxes = new CheckBox[cbNames];
for ( int i = 0; i < checkBoxes.length; i++ ) {
checkBoxes[i] = new CheckBox(cbNames[i], false);
checkBoxes[i].addItemListener(this);
}
You get the idea?
~Bill
# 2
Alternatively, you could add an ItemListener to each checkbox that will add/remove itself from a List of selected items. You can then query for the contents of the List at anytime without looping through all of your checkboxes. (Of course, you'll still be "looping" through the List, but that's a bit different). Oh, and it also eliminates the need for e.getSource() and other similar monstrocities.
You can still build an array of checkboxes rather than individually as I've done here. I just wanted to demonstrate the part where the ItemListener does the work.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class CheckBoxTest extends JFrame
{
JCheckBox chkChoice1 = new JCheckBox("English");
JCheckBox chkChoice2 = new JCheckBox("French");
JCheckBox chkChoice3 = new JCheckBox("German");
JCheckBox chkChoice4 = new JCheckBox("Italian");
JTextArea txaDisplay = new JTextArea(5, 20);
JButton btnShow = new JButton("Show Selected Items");
ArrayList <JCheckBox>choices = new ArrayList<JCheckBox>();
public CheckBoxTest()
{
super("");
setDefaultCloseOperation(EXIT_ON_CLOSE);
chkChoice1.addItemListener(new CheckBoxListener(chkChoice1));
chkChoice2.addItemListener(new CheckBoxListener(chkChoice2));
chkChoice3.addItemListener(new CheckBoxListener(chkChoice3));
chkChoice4.addItemListener(new CheckBoxListener(chkChoice4));
btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
txaDisplay.setText("");
for (JCheckBox jcb : choices)
{
txaDisplay.append(jcb.getText() + "\n");
}
}
});
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(chkChoice1);
buttonPanel.add(chkChoice2);
buttonPanel.add(chkChoice3);
buttonPanel.add(chkChoice4);
JScrollPane scroll = new JScrollPane(txaDisplay);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
JPanel dataPanel = new JPanel();
dataPanel.add(btnShow);
dataPanel.add(scroll);
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(buttonPanel, BorderLayout.NORTH);
c.add(dataPanel, BorderLayout.CENTER);
pack();
}
public static void main( String[] args )
{
CheckBoxTest frame = new CheckBoxTest();
frame.setVisible(true);
}
class CheckBoxListener implements ItemListener
{
JCheckBox checkBox;
public CheckBoxListener(JCheckBox checkBox)
{
this.checkBox = checkBox;
}
public void itemStateChanged(ItemEvent event)
{
if (checkBox.isSelected())
{
choices.add(checkBox);
}
else
{
choices.remove(checkBox);
}
}
}
}