Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to grayout non editable cell in JTable

853982May 6 2011 — edited May 6 2011
hi

I am developing a jtable application, I have some cells with null values. I have set those cells in non editable mode. Now I am trying to set gray background color of those cells.

I have used following code but It sets all cells (Editable also) in gray color.

JTable table = new JTable() {
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (!isCellEditable(row, column)) {
c.setBackground(Color.GRAY);
}
return c;
}
};

When I add the else part in condotion.like this

JTable table = new JTable() {
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (!isCellEditable(row, column)) {
c.setBackground(Color.GRAY);
}
else{
c.setBackground(Color.white);
}
return c;
}
};


then it sets the color properly but it does not highlight all cell text when I select it. and text also not visible in nimbus, windows and CDE/Motif look and feel.(because I set the background color is white).

Your assistance will be appreciated

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 3 2011
Added on May 6 2011
1 comment
1,403 views