I am just doing some trial and error right now..really just spit balling some stuff....but I have researched and can't find anything to do what I am looking to do. Right now I have a JFrame created with a bunch of checkboxes and a button. I am trying to just create a simple game where the person who checks the last checkbox wins. I have the box created and have got it to disable any already checked checkboxes. I am thinking that when the button is clicked it would then do some code to randomly check some boxes (computer plays against the human.) I am at the point where I think I need to be able to read the all the JFrames components and then create some if statements to figure out if a)the component is a checkbox and b) if that checkbox is unchecked..check it. I have been trying to use getComponents() with no success. I am just hoping for a nudge in the right direction.
Put the JCheckBoxes in an array:
Now you can access the array randomly to check them. I see two ways:
1) for X number of times get a random array element, select that. This does mean that you could potentially select the same one over and over.
2) for each element, use a random value to determine whether or not it should be selected. This does mean that you could potentially select them all, or none at all.
You can solve the problem of 1) by using a List and removing from that:
Remember to re-evaluate list.size() each time, as it will change when you remove elements.
I think I ought to move this thread to our GUIs forum.
Have you tried adding the check boxes to a ButtonGroup?
And move the actionPerformed method into a class on its own, and add that to the individual buttons.
Try an array of boxes, so you can create the array in a loop rather than individual lines.
While I was going through the API, I found two Java™ Tutorials links, which might be helpful: 12.