Jump to content
  • Advertisement
Sign in to follow this  
rcrampton

Java TableLayout Questions

This topic is 4438 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a turn based 2D tile playing game and have picked the TableLayout mamanger from Sun for the playing area. I have seen several references to this layout manger for this application so I am assuming it's been used successfully. I have a Tile class that extends JLabel and overrides paintComponent for painting. That seems to work well. I can't seem to figure out a couple of really simple things though: 1. How do I take a mouse click and figure out which row,col I clicked on? I have a mouseListener added and can get the x,y point in the panel but there must be an easy way to get the integer row,col from the table. 2. Once I have a row,col for the table how do I get the corresponding component I stuffed in there? The only way I've found is to use Container.getComponent(point x) which seems round-about given I've got a table based layout manager. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
If you are only concerned about getting the specific component that represents the tile you clicked on, you can just make each component share a mouse listener. Then when you click, you will have the x/y coordinate, and the event structure will have a reference to the object you clicked on.

Share this post


Link to post
Share on other sites
Thanks for the reply. I had thought of that awhile back but your reply brought it back to the front of my mind and solved the problem.

My data set is a matrix made up of ArrayLists. Each object in the matrix is unique, so if I relate the mouse event to an object I can get the X,Y that corresponds to it. it's not the fastest search algorithm but my data sets are very small so it's OK.

Thanks.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!