File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "adding JComponent to Grid" Watch "adding JComponent to Grid" New topic

adding JComponent to Grid

Sultan Ali

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?


Sultan Ali

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:


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

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

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

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

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

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..
I agree. Here's the link:
subject: adding JComponent to Grid
It's not a secret anymore!