• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help! Need to dynamically create JCheckBoxes

 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to dynamically create an unspecified number of JCheckBoxes, giving one a unique static value.
Because I don't know ahead of time how many JCheckBoxes I'll have to create, I thought that I could use one JCheckBox object and (in a loop as many times as necessary) give it unique settings and then adding the (hopefully) unique checkbox to the JPanel after each loop iteration.
This doesn't work.
What happens is that on screen I get checkboxes with different names, but their values and actions all correspond to the ones assigned to the last JCheckBox created. This makes sense, as all the JCheckBoxes point to the one JCheckBox object I created.
My question: Am I on the right track, or is there a better way to do this?
 
Dewang Shah
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
One option is to use a Collection(ArrayList or LinkedList) and add each checkbox to the collection. Then wherever you need to refer to the checkbox you can refer to it via the Collection index.

Originally posted by Matt Senecal:
[B]I need to dynamically create an unspecified number of JCheckBoxes, giving one a unique static value.
Because I don't know ahead of time how many JCheckBoxes I'll have to create, I thought that I could use one JCheckBox object and (in a loop as many times as necessary) give it unique settings and then adding the (hopefully) unique checkbox to the JPanel after each loop iteration.
This doesn't work.
What happens is that on screen I get checkboxes with different names, but their values and actions all correspond to the ones assigned to the last JCheckBox created. This makes sense, as all the JCheckBoxes point to the one JCheckBox object I created.
My question: Am I on the right track, or is there a better way to do this?
[/B]

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!