Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Graphic content that spans two cells in a JTable  RSS feed

 
Kai Jennings
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTable using a DefaultTableModel that results in tables like this:


However I am curious if there's a good way to create arrows in cells crossing over like in the example below...


I really just want:
(1) an arrow to cross from the bottom left corner of each B box to the top left corner of each A box, and
(2) an arrow to cross from the top left corner of each Y box to the bottom right corner of each X box. (I drew the green arrows wrong)

This seems difficult to me.
Is there any way maybe using a DefaultTableCellRenderer, or perhaps using awt.Paint package will be the way to go? Thoughts?
 
Rob Camick
Ranch Hand
Posts: 2754
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A custom renderer won't work because a render is only used to paint a single cell, not overlapping cells.

I would suggest you can do custom painting on top of the table after the default painting has been done:

This should get you started:


This will paint the diagonal line between the first and second column. You will need to draw the arrow head.
 
Kai Jennings
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:A custom renderer won't work because a render is only used to paint a single cell, not overlapping cells.

I would suggest you can do custom painting on top of the table after the default painting has been done:

This should get you started: ...


Thanks a lot for the starter! I ended up with this:

temp.png
[Thumbnail for temp.png]
 
Rob Camick
Ranch Hand
Posts: 2754
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks good.

I was just thinking you could create more reusable code by creating an ArrowIcon and then just paint the Icon on the table.




Now you can take advantage of the Rotated Icon class to do the rotation for you.

Then your (untested) table painting code can be simplified to something like:



The idea of this approach is that the tricky part of painting the Arrow is only done once. If you don't want to paint your own Icon you could even create a fancy arrow image in a painting application as use the image as the arrow Icon.

Then you can rotate the ArrowIcon at any angle you want without recreating your custom painting code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!