JTable - change row background color
I have the following code:
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
class ColoredTableCellRenderer
extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent
(JTable table, Object value,boolean selected,boolean focused,int row,int column)
{
setEnabled(table ==null || table.isEnabled());// see question above
if ((row % 2) == 0)
setBackground(Color.green);
else
setBackground(Color.lightGray);
super.getTableCellRendererComponent(table, value, selected, focused, row, column);
returnthis;
}
}
And
//JTable
String[] head ={"Namn","M錸dag","Tisdag","Onsdag","Torsdag","Fredag"};
String[][] data ={{"Niklas","9-15","9-15","9-15","9-15","9-15"},
{"Niklas","9-15","9-15","9-15","9-15","9-15"}};
JTable table =new JTable(data, head);
JScrollPane centerFrame =new JScrollPane(table);
//table.setEnabled(false);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
TableColumnModel tcm = table.getColumnModel();
TableColumn tm = tcm.getColumn(0);
tm.setCellRenderer(new ColoredTableCellRenderer());
I want the possibility to change background color to one row, column or cell, but as i have read on the net and taken code from the above code should change the color per row. And thats correct, but it changes the code only for the one column!
I want to change the entire row or column!
Plz help!
[3119 byte] By [
huba] at [2007-11-27 1:00:14]

Since a JTable with typically use multiple TableCellRenders, a better
approach to coloring across rows is to subclass JTable and override
prepareRenderer:
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component stamp = super.prepareRenderer(renderer, row, column);
if (it is time for red)
stamp.setBackground(Color.RED);
else
stamp.setBackground(this.getBackground());
return stamp;
}
Works for me. Try modifying this:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
class Stripey extends JTable {
public Stripey(Object[][] rowData,
Object[] columnNames) {
super(rowData, columnNames);
}
Color alternate = new Color(0xC0, 0xC0, 0xF0);
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component stamp = super.prepareRenderer(renderer, row, column);
if (row % 2 == 0)
stamp.setBackground(alternate);
else
stamp.setBackground(this.getBackground());
return stamp;
}
}
public class JTableExample implements Runnable {
public void run() {
Object[][] rowData = {
{"a","b","c"},
{"d","e","f"},
{"g","h","i"},
{"j","k","l"},
{"m","n","o"},
{"p","q","r"},
};
Object[] columnNames = {"alpha", "beta", "gamma"};
JFrame f = new JFrame("JTableExample");
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.getContentPane().add(new JScrollPane(new Stripey(rowData, columnNames)));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new JTableExample());
}
}
Alternatively, one can use this slightly more brittle approach:
import java.awt.Color;
import javax.swing.JTable;
import javax.swing.table.*;
/**
* GrayWhiteTable renders its rows with a sequential color combination of white
* and gray. Rows with even indicies are rendered white, odd indicies light grey.
* Note: Do not use GrayWhiteTable for tables with custom renderers such as
* check boxes. Use JTable instead and modify DefaultTableCellRenderer. Just keep
* in mind that in order to display a table with more than 1 row colors, you
* must have 2 separate intances of the renderer, one for each color.
*/
public class GrayWhiteTable extends JTable
{
private DefaultTableCellRenderer whiteRenderer;
private DefaultTableCellRenderer grayRenderer;
public GrayWhiteTable()
{
super();
}
public GrayWhiteTable(TableModel tm) {
super(tm);
}
public GrayWhiteTable(Object[][] data, Object[] columns)
{
super (data, columns);
}
public GrayWhiteTable(int rows, int columns)
{
super (rows, columns);
}
/**
* If row is an even number, getCellRenderer() returns a DefaultTableCellRenderer
* with white background. For odd rows, this method returns a DefaultTableCellRenderer
* with a light gray background.
*/
public TableCellRenderer getCellRenderer(int row, int column)
{
if (whiteRenderer == null)
{
whiteRenderer = new DefaultTableCellRenderer();
}
if (grayRenderer == null)
{
grayRenderer = new DefaultTableCellRenderer();
grayRenderer.setBackground(Color.lightGray);
}
if ( (row % 2) == 0 )
return whiteRenderer;
else
return grayRenderer;
}
}