I need to display basic graphic elements for a research project. These
elements will be displayed as a grid (for example 128 X 128 squares,
each square containing a different element).
The only thing I might say works against the JTable
is that it was designed that each column would be the same object type.
This doesn't mean it's impossible, but it requires more work with dealing with cell renderers and updating the individual values.
I want to be able to easily access elements on the grid by coordinates,
This depends on your initial entry point. For example, if you only have a mouse point, you can ask the table for the row and column which represents the given point, for example...
Point p = mouseEvent.getPoint();
int row = table.rowAtPoint(p);
int col = table.columnAtPoint(p);
Once you have that, you can simply get the cell value from the table using something like...
Object cellValue = table.getValueAt(row, col);
redraw only that particular cell
Generally, this can be achieved through the TableModel
, which should be notifying the table that some part of the table has updated and should be repainted.
For example, in the AbstractTableModel
, there are a number of "event" methods which can be used to generate events that tell the table that some part of the model has changed.
For example, you could use public void fireTableCellUpdated(int row, int column) to tell the table that a given cell has changed and the table will react to it by re-painting the given cell. This is typically called by the model.
and I think I'll have to move logic into rendering code (or use wrappers).
The table API (and in fact many of the advance components in Swing) uses a "renderer" to allow programs to define how certain elements within the component should be displayed.
Take a look at Using custom renderers for more details...
Generally speaking, the JTable
is a complex API, but the complexity comes from it's in built flexibility. The more you use it, the more uses you will find for it...
Take a closer look at How to use tables for more details.