wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes adding JComponent to Grid Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "adding JComponent to Grid" Watch "adding JComponent to Grid" New topic
Author

adding JComponent to Grid

Sultan Ali
Greenhorn

Joined: Jul 21, 2011
Posts: 7
Hi Everyone, Am currently working on a Checkers game (English draughts) to upgrade my java skills, But am having Slight difficulties placing the pieces into the Board and I am unsure of how i could go about it, i know i would have to store all the pieces into an array then loop through the array and add it to the Board But since the pieces are all JComponent (Filloval) i dont know how i could store Jcomponents into an array, Is it possible to add a number of fillovals into an Arraylist?. i tried this but it doesn't work, I would appreciate any Advice of how i could go about it?

cheers





Sultan Ali
Greenhorn

Joined: Jul 21, 2011
Posts: 7
My Main problem is how to store a list of JComponent into arraylist i tried to add it but it wouldnt work, Does anyone have a Solution??
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

You can store JComponents in an ArrayList just like any other object in Java:

try specifying the type of the ArrayList when you create it before added components:



Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
... and welcome to the Ranch
Yasin Kothia
Ranch Hand

Joined: Sep 25, 2009
Posts: 39
as mentioned above.



If you have already tried this and are getting errors. let us know.
Sultan Ali
Greenhorn

Joined: Jul 21, 2011
Posts: 7
Many thanks for the kind responds,
I got it working but unsure if my method is correct but atleast is working, i simply defined the x and y positions of each J component i needed fillovals in my piece class
e.g




i then called it in my main class as follow



This seems to work at the moment but if you guys have any better way then please..
 
 
subject: adding JComponent to Grid