my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes searching JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "searching JFrame" Watch "searching JFrame" New topic
Author

searching JFrame

john-paul York
Greenhorn

Joined: Feb 15, 2011
Posts: 16
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

Joined: Oct 27, 2005
Posts: 19755
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
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

Joined: Oct 27, 2005
Posts: 19755
    
  20

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

Joined: Oct 13, 2005
Posts: 39755
    
  28
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

Joined: Feb 15, 2011
Posts: 16
Thanks guys for the info...as always your friendly and quick

cheers

JP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: searching JFrame