• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

searching JFrame

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80288
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80288
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for the info...as always your friendly and quick

cheers

JP
 
I found a beautiful pie. And a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic