Two Laptop Bag
The moose likes Swing / AWT / SWT and the fly likes Row Highlight on a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Row Highlight on a JTable" Watch "Row Highlight on a JTable" New topic

Row Highlight on a JTable

John Jai

Joined: May 31, 2011
Posts: 1776

I want to highlight the entire row when a user clicks on a cell of a JTable

My JTable has following -> CheckBox in column 0; strings in columns 1,2,3; Combo Box (Custom Rendered) in col 4, and other strings in col 5,6,7.

Also the Table has a Select All Check Box (Rendered using Custom CheckBoxHeaderRenderer).

Since there are multiple custom renderers used in the Table i need guidance on below few things.

1. Can I HIGHLIGHT (i.e. change ForeGround to selection foreground color) for the ENTIRE ROW when i click the checkbox in Column 1? - Since the Table is big in width this will allow users to know which row they have just selected?

2. Can I HIGHLIGHT the entire row when i click on the ComboBox present in col 4 - This is rendered using a Custom combo box renderer?

3. Can I HIGHLIGHT the entire row when i click any of the other cells (normal string value cells) present in the row?

4. Can I HIGHLIGHT the entire table (all rows & columns) when I select the Check Box present in my Column 1 HEADER of the JTable?

Apologies if my question is misleading!
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Sure, you can do anything you like in your TableCellRenderer. In particular your renderer knows whether the row containing the cell is selected -- providing your table's selection model works by rows. It can even look at other information, like for example that check box in column 1 header, and act accordingly.
John Jai

Joined: May 31, 2011
Posts: 1776
Hi Paul,

Thanks for your reply. For now I think the highlight of the entire row should be OK when a particular cell is selected. After seeing your reply i just changed my selection mode to row selection by commenting cell selection like below.

// this.setCellSelectionEnabled(true);

Now this highlights the whole row which suffices my requirement for now. Thanks much!!!

I agree. Here's the link:
subject: Row Highlight on a JTable
It's not a secret anymore!