Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

searching JFrame

 
john-paul York
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 1 2.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I recall correctly, a ButtonGroup will allow only one selected button. That's not what John-Paul wants; he wants to have all check boxes selected in the end.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke points out the same to me. I had missed that you need all your boxes ticked, so, as you say, a Buitton group would not work here.
 
john-paul York
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for the info...as always your friendly and quick

cheers

JP
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic