permaculture playing cards
The moose likes Swing / AWT / SWT and the fly likes JTable Cell Merge Highlighting and Redrawing Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Cell Merge Highlighting and Redrawing Issue" Watch "JTable Cell Merge Highlighting and Redrawing Issue" New topic

JTable Cell Merge Highlighting and Redrawing Issue

Brandon Murphy

Joined: Dec 20, 2010
Posts: 4
I have been working on getting cell merging to work with a JTable, and in order to do this I have had to rewrite all of BasicTableUI using its source code as a template. I have also needed to override the getCellRect method of JTable, and have created a custom CellRenderer. With that being said, the problem I have run into is that when I merge a group of cells together everything works until I go to highlight an area around that group of cells. When I do this the area that would have been highlighted had no cells been merged is cleared everywhere within that cell merger. That is probably confusing so I have included a few images:

This is an image of the table with a cell merger:

And these images are when I highlight some area around the cell merger that include part of the merger within the highlightings bounds (notice how some of the text is erased, this is the problem):

And it might be worth showing you that everything works fine when the entire cell merger is highlighted:

I should also mention that when cells are merged, the upper left hand cell take precedence, in other words, that cell is enlarged to the size of the cell merger and the other cells are simply never drawn (I took care of this in BasicTableUI).

I am presently unsure where to go from here to fix this issue. I was thinking it might have something to do with RepaintManager but I am unsure. If someone could point me in the right direction I would very much appreciate it. If you need any additional information please let me know.

Thank you for your time,
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2451

Be Forthright When Cross Posting To Other Sites
Brandon Murphy

Joined: Dec 20, 2010
Posts: 4
Ah, I will follow this in the future. If i receive an answer at Java-Forums I will post it here as well.
Darryl Burke

Joined: May 03, 2008
Posts: 5115

If i receive an answer at Java-Forums

That would be

luck, db
There are no new questions, but there may be new answers.
Martin Vajsar

Joined: Aug 22, 2010
Posts: 3733

I've done something similar in our project.

First, you have to define how do you want the table to behave when selecting merged cells. We've used Excel as a model. Generally in Excel, when a merged cell becomes part of the selection, the selection is automatically expanded to include all merged cells that are part of it. I'd suggest to experiment with Excel first and then precisely formulate the goal, or devise your own set of rules from scratch (however, I'd say there are not so many alternative, yet internally consistent solutions around). Be sure you cover all possible configurations of merged cells, otherwise a layout that was not accounted for might pop later on and you'll be redoing it all from the start.

(Another option might be to draw merged cells as partially selected, but it would somewhat defeat the goal of having them merged in the first place.)

Second, with the rules firmly set, you'll have to intercept the inner mechanism of navigation and selection in a JTable and implement the rules. This may be even more work than overriding the painting, since the relevant code is spread across several methods and most of them you normally don't need to touch.

At this moment I'm not able to give more specific suggestions. It's been a few month since I've done this, and parts of the navigation were done by a colleague that since left. However, I might be able to help with specific problems you may encounter in the future.

Examples that were posted in your thread on OTN might also help.
I agree. Here's the link:
subject: JTable Cell Merge Highlighting and Redrawing Issue
jQuery in Action, 3rd edition