0

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 displaying part of the application is secondary to the logic behind it so I'm looking for the easiest way to display such a grid (while using Swing). I've considered JTable so far, but seems clunky. I would've liked to use something web-based but that does not suit the application.

Are there any libraries or better components for such drawing in basic Java/Swing?

Eugen
  • 1,537
  • 7
  • 29
  • 57
  • Please explain why you consider `JTable` based implementation "*clunky*". – PM 77-1 Jan 06 '14 at 00:41
  • I might not have thought it right, but it feels like JTable doesn't offer the flexibility I want. I want to be able to easily access elements on the grid by coordinates, redraw only that particular cell and I think I'll have to move logic into rendering code (or use wrappers). All in all, I'm trying to figure out if a JTable implementation will cause fewer headaches in the long run rather than something like offset layouts – Eugen Jan 06 '14 at 00:48
  • Well, there is also `GridLayout`, but like @PM77-1, my first thought was `JTable`. – Andrew Thompson Jan 06 '14 at 00:48
  • Somewhat similar post - [Display a big number of images into a grid](http://stackoverflow.com/questions/11763435/display-a-big-number-of-images-into-a-grid?rq=1). – PM 77-1 Jan 06 '14 at 00:58
  • 2
    JTable check list - *"I want to be able to easily access elements on the grid by coordinates"* - check, can do, both by x/y and through mouse point conversion. *"redraw only that particular cell and I think I'll have to move logic into rendering code (or use wrappers)."* - Check, can do. It might "seem" more difficult, but it is one of the core and common components you will use, well worth the time to learn. Check out [How to use tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) for more details... – MadProgrammer Jan 06 '14 at 01:14

1 Answers1

2

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.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Excellent and detailed answer. Thanks for taking the time! JTable it is. – Eugen Jan 06 '14 at 01:35
  • 1
    `JTable` is incredibly powerful API and well worth the time to learn, even if it doesn't end up meeting your requirements, it will be time well spent ;) – MadProgrammer Jan 06 '14 at 01:44