This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable Cell Renderer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Cell Renderer" Watch "JTable Cell Renderer" New topic
Author

JTable Cell Renderer

Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
Hi,

I have a JTable to which i intend to set the background color for every alternative row (irrespective of the values in each cell). I know the mehod for setting the background (of a cell alone) based on some condition present in each cell. But what i want to do is to set the colour of every cell (ie a row) . Could somebody please help me with this.

Thanks.
Anand Loni
Ranch Hand

Joined: Jan 20, 2006
Posts: 150
Hi,

Write your own renderer for the table in which you can write following code (code that set color red ,black alternately)


class MyTableRenderer extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(row%2 == 0)
{
cell.setBackground(Color.red);
}
else
{
cell.setBackground(Color.black);
}
return cell;
}
}
Regards,
Anand


~ Anand,
SCJP 1.5, SCWCD 1.5
Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
Hi,
There is something wrong with this. Its not working.



Any idea how to instantiate the class MyTableRenderer ?
Anand Loni
Ranch Hand

Joined: Jan 20, 2006
Posts: 150
Hi,
Just try this...

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
//import layouts.GridPanel;

public class TableTest extends GridPanel
{
final String[] colHeads =
{ "SNo", "col1", "col2", "col3", "col4" };

final Object[][] data =
{
{ "1", "a", "d", "b", "c" },
{ "2", "a", "d", "b", "c" },
{ "3", "a", "d", "b", "c" },
{ "4", "a", "b", "c", "d" },
{ "", "", "", "", "" },
{ "", "", "", "", "" },
{ "", "", "", "", "" },
{ "", "", "", "", "" },
{ "", "", "", "", "" } };


TableTest()
{
super();
JTable jtbl = new JTable(data, colHeads);
JScrollPane jsp = new JScrollPane(jtbl);
jtbl.setDefaultRenderer(Object.class, new MyTableRenderer());

this.setSize(640, 600);
this.setBackground(new Color(225, 232, 219));
jsp.setViewportView(jtbl);// table on scroll pane
this.addFilledComponent(jsp, 1, 1, 5, 2, GridBagConstraints.BOTH);
}// constructor

class MyTableRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (row % 2 == 0)
{
cell.setBackground(Color.red);
}
else
{
cell.setBackground(Color.black);
}
return cell;
}
}
}
Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
Morning Anand.

Thanks a lot for pointing out that bug.
jtbl.setDefaultRenderer(Object.class, new MyTableRenderer());
I messed up this line completely.

Have a great day !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable Cell Renderer
 
Similar Threads
JTable
painting JTable rows
JTable
JTable
JTable Trouble