Hi all! It seems this is a good forum to post my question. It's not
difficult and I don't ask for code as I'd like to play about with a
problem for some time myself. However, perhaps you guys could help me
with answering three general questions.
At the moment I am having to create a gird, or sort of a 2D map for my
application. I've worked a lot with Java throughout the years and
already have some solutions to my problem. However, as a useful exercise
I was thinking of implementing this using a JTable and I am wondering
how difficult this is as I have little experience with JTables. At the
moment I am working my way through some JTable tutorials (custom table
models etc).
So, specific questions are:
1. Is it easy to draw polygons, arcs and circles in a cell in a table,
possibly by using an overriding paint(Graphics g) method? A simple yes
or no would do as I would quite enjoy the challenge of figuring it out.
At the moment I am thinking something along these lines, where JEntity
is a JLabel with it's own overriding paint method:
public class EntityTableCellRenderer extends JEntity implements
TableCellRenderer { // Normal code stuff goes here }
.
2. Is it easy to implement cells that can hold objects? Say, if an
earthquake hit an area, i.e. a cell, can the cell trigger some event
that is propagated to objects? Could an Observer pattern be used here,
so the objects observe the cell, or are there better ways of
notification? Any hints would be great.
3. Can a JPanel be easily added to a cell?
Thanks all, you're doing a great job with helping people here,
Sven
VisionSet - 25 Dec 2004 00:44 GMT
> 1. Is it easy to draw polygons, arcs and circles in a cell in a table,
> possibly by using an overriding paint(Graphics g) method? A simple yes
[quoted text clipped - 3 lines]
> public class EntityTableCellRenderer extends JEntity implements
> TableCellRenderer { // Normal code stuff goes here }
Yes, quite straightforward. A JTable uses TableCellRenderer to draw the
contents. These can be set through methods of Jtable. And a
TableCellRenderer can return the necessary Component for drawing.
> 3. Can a JPanel be easily added to a cell?
The cell is a Component, all you need is a handle on that component amd you
can add what you like. Not sure how the layout will behave though, try it
and see.

Signature
Mike W
Chris Smith - 26 Apr 2005 06:44 GMT
> So, specific questions are:
>
[quoted text clipped - 6 lines]
> TableCellRenderer { // Normal code stuff goes here }
> .
Yes, that works just fine.
> 2. Is it easy to implement cells that can hold objects? Say, if an
> earthquake hit an area, i.e. a cell, can the cell trigger some event
> that is propagated to objects? Could an Observer pattern be used here,
> so the objects observe the cell, or are there better ways of
> notification? Any hints would be great.
Yes, table cell values can be any arbitrary object. The cell renderer
just needs to be able to understand the object and provide an
appropriate visual representation. If you use the default renderer,
then that means the object should override toString... but if you plan
to install your stick figure renderer, then even that requirement is
gone.
> 3. Can a JPanel be easily added to a cell?
No, but you can return a JPanel instance from a cell renderer.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Chris Smith - 21 Jun 2005 01:59 GMT
> 1. Is it easy to draw polygons, arcs and circles in a cell in a table,
> possibly by using an overriding paint(Graphics g) method? A simple yes
> or no would do as I would quite enjoy the challenge of figuring it out.
Easy is relative... but I'd have a hard time convincing anyone that it's
hard! You're on the right track.
> 2. Is it easy to implement cells that can hold objects? Say, if an
> earthquake hit an area, i.e. a cell, can the cell trigger some event
> that is propagated to objects? Could an Observer pattern be used here,
> so the objects observe the cell, or are there better ways of
> notification? Any hints would be great.
Your data model can do whatever it wants. Note that you should develop
the data model (all this stuff) first, and *then* retrofit
implementations of the Swing TableModel and TableCellRenderer interfaces
on top of it. If you feel like having an object representing each cell
and having it fire events according to the JavaBeans event idiom, then
go for it.
> 3. Can a JPanel be easily added to a cell?
Careful here; *nothing* can be sensibly added to a JTable. Instead, the
TableCellRenderer returns something that is used as a rubber stamp to
draw the visual appearance of each table cell. The relationship between
that object and the JTable is one of use and reference, not containment.
Yes, a JPanel may be used as that rubberstamp to render a JTable cell.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation