Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

adding JComponent to Grid

 
Sultan Ali
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch
 
Yasin Kothia
Ranch Hand
Posts: 41
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as mentioned above.



If you have already tried this and are getting errors. let us know.
 
Sultan Ali
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic