• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with dynamically created JCheckBox

 
KathySmith SmithL
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I'm new to Swing and I'm still learning and experimenting with the controls. I have dynamically created Checkboxes based on the resultSize. I need to store the selected checkboxes into an array or Vector. I am unable to find out which ones are selected by the user. Can someone help me with this problem?
--------------------------------------
I have a dynamically created chkboxes like this:
// some code....
Container cont = getContentPane();
JPanel jp = (JPanel) cont.getComponent(0);
JTabbedPane jtbp = (JTabbedPane) jp.getComponent(0);
jp = (JPanel) jtbp.getComponent(1);
int rsltSize = srchrslts.size();

if (rsltSize != 0) {
for (int i = 0; i < rsltSize; i++) {
jChkBox_results = new JCheckBox();
jChkBox_results.setName("jChkBox_results"+ (i+1));
jChkBox_results.setPreferredSize(new Dimension(273, 17));
jChkBox_results.setText(srchrslts.get(i).toString());
jp.add(jChkBox_results);
}

JButton btn_ShowSoln = new JButton();
btn_ShowSoln.setText("Show Solutions");
jp.add(btn_ShowSoln);
btn_ShowSoln.addActionListener(new GUIFrame_btn_ShowSoln_actionAdapter(this));

this.validate();
this.setVisible(true);
}
else {
// send an alert - no results found
createAlert();
}



In btn_showSoln, I need to store the selected checkboxes into a vector to process later

void btn_ShowSoln_actionPerformed(ActionEvent e) {
try {
for(int i = 0; i < rsltSize; i++) {
if(jChkBox_results.getName().equals("jChkBox_results" + (i+1))){
System.out.println("Selected chk boxes are : ");
System.out.println(jChkBox_results.getName());
}
}

}
catch (NullPointerException npe) {
//display alert to user saying nothing was checked.
System.out.println("Please check atleast one record !!!");
}
}

-----------------------------

Everytime I get the last added checkbox name printed n number of times depending on the resultSize.. is there any other way to get the selected checkboxes??

Thanks in advance
Kathy
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this might be one way

 
KathySmith SmithL
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.. it worked.. my chkboxes were not an arrayList... hence I tried it in a different way.. used a Vector instead of the arrayList to store the individual checkboxes created and checked if each element in the vector was selected.

Kathy
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic