aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Row Rendering in 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 Rendering in JTable" Watch "Row Rendering in JTable" New topic
Author

Row Rendering in JTable

RAJEEV BAHL
Greenhorn

Joined: Apr 26, 2001
Posts: 15
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

Joined: May 17, 2001
Posts: 2823
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

Joined: Apr 26, 2001
Posts: 15
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Row Rendering in JTable
 
Similar Threads
JTable
Rotating an imageicon in JTable cells
How to Set the JTable Rows Color??
Problem with JTable
How to give different foreground color for the same cell in JTable?