Sign in to follow this  

Java TableLayout Questions

This topic is 4223 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
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

This topic is 4223 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this