This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Need help with dynamically created JCheckBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need help with dynamically created JCheckBox" Watch "Need help with dynamically created JCheckBox" New topic
Author

Need help with dynamically created JCheckBox

KathySmith SmithL
Greenhorn

Joined: Oct 17, 2004
Posts: 10
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

Joined: Jun 09, 2003
Posts: 4632
this might be one way

KathySmith SmithL
Greenhorn

Joined: Oct 17, 2004
Posts: 10
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
 
wood burning stoves
 
subject: Need help with dynamically created JCheckBox
 
Similar Threads
An interesting Program
Why I cannot undo this??
MIDI Network Program Error
Visibility help?
getting a JScrollPane to display its scrollbars