Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Row Rendering in JTable

 
RAJEEV BAHL
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is code for my problem. I am able to render JTable column wise but unable to achive row rendering ie redering particular rows as per my requirement, which as per books from sun can be performed as per RowRenderer class. In my case all columns and rows get same foreground and background Color.

JTable jTable = new JTable(15, 4);
// some data added to table using jTable.setValueAt((Object), row, col)
TableColumn jTableCol;
for (int i = 0; i < jTable.getColumnCount(); i++)
{
jTableCol = jTable.getColumn(jTable.getColumnName(i));
if (i == 0)
{
jTableCol.setCellRenderer(new ColRenderer());
jTableCol.setPreferredWidth(70);
}
else
jTableCol.setCellRenderer(new RowRenderer());
}
private class ColRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent(JTable jTable, Object value, boolean isSelected, boolean hasFocus, int row, int col)
{
setForeground(Color.white);
setBackground(Color.lightGray);
return super.getTableCellRendererComponent(jTable, value, isSelected, hasFocus, row, col);
}
}

private class RowRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent(JTable jTable, Object value, boolean isSelected, boolean hasFocus, int row, int col)
{
if ((row == 3) && (row == 13))
{
setForeground(Color.white);
setBackground(Color.lightGray);
}
else
{
setForeground(Color.black);
setBackground(Color.white);
}
return super.getTableCellRendererComponent(jTable,value, isSelected, hasFocus, row, col);
}
}
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why use 2 renderers. Both are going to be called. So the col is probably overriding the row. Just calling it RowRenderer doesn't make it so. Do your row and column checking within 1 renderer and set the colors.
 
RAJEEV BAHL
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Paul,
I tried even that ie doing everything in one class. But whenever I try to render single row all Table get redered by same color. My intenstion is to render one column and two rows to be precise.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic