My target is to display 256 images in a 16x16 grid (as the subject of this topic already hints at). This grid should be positioned in a container. When clicking with the mouse on one of the images I want to know on which image the click was made, and that in a way that makes it easy to get the coordinates (0,0 being the top left image, 15,15 the bottom right). I use NetBeans and JSE 6.
As a beginner I would like to get a hint to the following preliminary thoughts I had.
a. Use labels (JLabel). Problem: You have to generate 256 of them. Not very attractive to do with NetBeans - and you have no elegant way to get the coordinates back (using the name of JLabel - JLabel3x5, JLabel15x15 - seems very awkward
, using the graphic coordinates is not reliable if the window is resized). Even if you do not use NetBeans and generate the labels manually, say in an array, the coordinate problem remains.
b. Use a table (JTable), display the images in a grid and prevent the cells from being editable. As far as I got it you can define your own TableModel and include something like
If I read it right I can add a listener which catches the MouseEvent my mouse makes while clicking in a cell and get the coordinates of it (getSelectedRow (), getSelectedColumn ()).
Is there another possibility which is better suited? Using a table seems a bit cheaty to me, because you usually expect a table to be editable at least a bit and not only showing pretty pictures of rooms and tunnels and other dungeon stuff (which it would in my case).
Input would be greatly appreciated.