Link to home
Start Free TrialLog in
Avatar of macgre
macgreFlag for Canada

asked on

How to draw an optional border around a JTable row?

How would I go about drawing a border around a row in a JTable?  The default behaviour is cell based, I don't want to see the cell borders but I do want an optional border around a row.  I have been able to make the cell borders disappear but have not been able to figure out how to get a border around the entire row.

At the moment I am considering providing custom cell renderers to draw the top and bottom borders and depending on the column draw either the left or right border.  In other words draw the border in parts.

I don't like this solution and am wondering if there is a better easier way...
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

I would pursue the way you've already identified
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of macgre

ASKER

objects,

If I understand your code correctly you are expanding the insets object of a cell to encapsulate a selected block of cells.  I was to use the code from the page you listed because there were three imports not included in the page, so I couldn't do my own testing...

I have a list of people where some may be "special" and my client wants me to draw a red border around them, they did not like it when I showed them each cell with a red border...  Your solution seems tied to a selection, I would like to test if I can have multiple, non-continuous rows with a red border.

I may end up doing a combination of both, that is a custom renderer that uses an insets object to draw a border around the entire row, getting its dimensions from the viewport width and row height.
Avatar of macgre

ASKER

I went with a custom renderer, it wasn't as hard as I thought it would be...
:-)
> I was to use the code from the page you listed because there were three imports not included in the page, so I couldn't do my own testing...

they are also there

http://www.objects.com.au/java/examples/swing/table/BorderCellRenderer.java
http://www.objects.com.au/java/examples/swing/table/CellBorder.java

> that is a custom renderer that uses an insets object to draw a border around the entire row, getting its dimensions from the viewport width and row height.

which is what the example I posted above already does :)
So not exactly clear why you ignored my comment which gave you an example of what you mentioned trying, and instead accepted comments that just agreed with you without providing any material that would actually help.
guess I should have said, "I agree, heres some code to get you started" :-D
Thx
Avatar of macgre

ASKER

objects,

Did not intend to offend, I could not get your example to work so I went with my original idea.  I tried to incorporate what I saw in your code but couldn't get the results I wanted.  In the end I built three different renderers, one for each edge and one for the middle and used the column index to decide which one to use.  I extended DefaultCellerRenderer and overrode the paintBorder method of the LineBorder class three times (called LEFT, MIDDLE and RIGHT) and used the appropriate one from the getTableCellRendererComponent.  Since this looks nothing like what you provided, I couldn't see how I could take points away from those that suggested that my original plan was the correct one.

I will take what you have given and try again later, if I can get it working and it is an improvement over what I have now I will post a question directed at you personally and reward you for your efforts.  I do appreciate your help, you have been very helpful in the past and I do appreciate your input, I just couldn't use it this time...
I suggested ujsing a custom cell renderer as you were also considering, and gave u an example of such. Just fail to see how that is less helpful than someone simple posting "I agree".

> I extended DefaultCellerRenderer and overrode the paintBorder method of the LineBorder class three times (called LEFT, MIDDLE and RIGHT) and used the appropriate one from the getTableCellRendererComponent.

what i posted does basically the same thing, just improves on that a little by delegating the painting of the border (to avoid having to override).

Will just agree with you next time ;)
Avatar of macgre

ASKER

It seems I didn't understand your code as well as I thought, not being it to get it to run didn't help either, still I prefer to have code to test with than not ;-)
Avatar of macgre

ASKER

Couple of questions:

1) The JTable renders each cell separately, does your example draw the row border once for every cell in the table?

2) Your solution seemed tied to a selection I would need something that draws a border based on data associated with the row but not displayed.  I have an object that supplies data to the table, but there are additional attributes that are not being displayed but would cause the row border to be red.