How to Correctly Use Custom Renderers to Paint Specific Cells in a Jtable

How do I correctly use custom renderers to paint specific cells in a JTable?

Add an else clause to your if:

if ((row == 0) && (column == 0)) {
d.setBackground(new java.awt.Color(255, 72, 72));
}
else {
d.setBackground(Color.WHITE);
}

Remember that the same renderer instance is used to paint all the cells.

How do I color individual cells of a JTable based on the value in the cell?

There is a relationship between the data/model and the view/table. The model maintains the "what", the view controls the "how".

JTable provides a means by which you can the behaviour of the "how" (stuff gets rendered) through the use of TableCellRenderers, these are responsible for determining how a cell should be "painted" based on the value from the "what" from the model.

Start by having a look at How to Use Tables and Using Custom Renderers

Now, this example uses a double value to determine the distance from 1 the cell value is, which represents the color (black = 0; white = 1) that the cell should be painted. To accomplish this, it uses a custom TableCellRenderer which converts the value in the model (the "what") into a color (or the "how")

public class PaintTableCellRenderer extends DefaultTableCellRenderer {

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, "", isSelected, hasFocus, row, column);
if (value instanceof Double) {
double distance = (double) value;
int part = (int) (255 * distance);
Color color = new Color(part, part, part);
setBackground(color);
} else {
setBackground(Color.WHITE);
}
return this;
}

}

Smile

It also demonstrates a couple of other things you might need to know about the JTable as well.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

public class Smile {

public static void main(String[] args) {
new Smile();
}

public Smile() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private double[][] smily = {
{1, 1, 1, 1, 1, 0.996, 1, 0.843, 0.784, 0.788, 0.773, 0.769, 0.765, 0.765, 0.788, 0.784, 0.847, 1, 0.996, 1, 1, 1, 1, 0.996},
{1, 1, 1, 1, 1, 0.871, 0.733, 0.761, 0.847, 0.941, 0.941, 0.941, 0.941, 0.941, 0.933, 0.843, 0.761, 0.733, 0.871, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0.784, 0.733, 0.902, 0.941, 0.941, 0.941, 0.945, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.898, 0.733, 0.784, 1, 1, 1, 1},
{1, 1, 1, 0.765, 0.773, 0.945, 0.945, 0.941, 0.929, 0.937, 0.941, 0.941, 0.945, 0.941, 0.957, 0.91, 0.941, 0.941, 0.941, 0.78, 0.761, 1, 1, 1},
{1, 1, 0.808, 0.773, 0.941, 0.941, 0.941, 0.941, 0.294, 0.447, 0.941, 0.941, 0.941, 0.941, 0.702, 0.239, 0.886, 0.941, 0.945, 0.941, 0.78, 0.8, 1, 1},
{1, 0.89, 0.725, 0.945, 0.941, 0.941, 0.945, 0.843, 0, 0, 0.922, 0.945, 0.941, 0.941, 0.408, 0, 0.663, 0.941, 0.941, 0.945, 0.941, 0.725, 0.89, 1},
{0.992, 0.753, 0.902, 0.941, 0.945, 0.945, 0.941, 0.725, 0.051, 0, 0.808, 0.941, 0.941, 0.945, 0.294, 0.051, 0.553, 0.941, 0.941, 0.941, 0.941, 0.91, 0.741, 0.984},
{0.871, 0.78, 0.941, 0.941, 0.945, 0.945, 0.941, 0.694, 0.051, 0, 0.784, 0.945, 0.941, 0.941, 0.278, 0, 0.518, 0.941, 0.941, 0.945, 0.941, 0.941, 0.78, 0.878},
{0.816, 0.855, 0.941, 0.945, 0.945, 0.945, 0.941, 0.737, 0, 0.051, 0.82, 0.941, 0.941, 0.941, 0.302, 0.051, 0.565, 0.941, 0.949, 0.945, 0.941, 0.941, 0.863, 0.804},
{0.8, 0.945, 0.941, 0.945, 0.945, 0.941, 0.941, 0.875, 0, 0, 0.937, 0.941, 0.941, 0.941, 0.443, 0, 0.694, 0.945, 0.945, 0.945, 0.945, 0.949, 0.941, 0.765},
{0.769, 0.941, 0.945, 0.957, 0.961, 0.941, 0.945, 0.941, 0.443, 0.565, 0.945, 0.941, 0.941, 0.941, 0.769, 0.388, 0.918, 0.941, 0.941, 0.941, 0.945, 0.941, 0.941, 0.78},
{0.753, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.953, 0.941, 0.941, 0.941, 0.941, 0.945, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.788},
{0.741, 0.945, 0.839, 0.427, 0.624, 0.941, 0.941, 0.945, 0.941, 0.941, 0.941, 0.949, 0.945, 0.945, 0.941, 0.941, 0.941, 0.941, 0.941, 0.6, 0.376, 0.941, 0.945, 0.784},
{0.749, 0.941, 0.914, 0.345, 0.647, 0.941, 0.945, 0.949, 0.945, 0.945, 0.941, 0.941, 0.945, 0.941, 0.945, 0.945, 0.945, 0.945, 0.941, 0.702, 0.384, 0.941, 0.941, 0.78},
{0.796, 0.945, 0.941, 0.627, 0.592, 0.941, 0.941, 0.941, 0.945, 0.945, 0.945, 0.941, 0.949, 0.945, 0.941, 0.945, 0.945, 0.937, 0.945, 0.58, 0.631, 0.941, 0.937, 0.776},
{0.812, 0.859, 0.941, 0.855, 0.384, 0.957, 0.941, 0.945, 0.945, 0.945, 0.941, 0.953, 0.941, 0.945, 0.945, 0.945, 0.941, 0.941, 0.941, 0.384, 0.941, 0.941, 0.867, 0.812},
{0.871, 0.788, 0.941, 0.941, 0.533, 0.51, 0.941, 0.941, 0.941, 0.945, 0.949, 0.945, 0.945, 0.945, 0.941, 0.937, 0.941, 0.945, 0.522, 0.522, 0.941, 0.941, 0.792, 0.886},
{0.992, 0.761, 0.914, 0.941, 0.941, 0.325, 0.612, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.624, 0.318, 0.941, 0.945, 0.91, 0.765, 0.988},
{1, 0.882, 0.741, 0.941, 0.941, 0.922, 0.337, 0.475, 0.894, 0.941, 0.941, 0.941, 0.941, 0.941, 0.945, 0.894, 0.49, 0.325, 0.925, 0.941, 0.941, 0.753, 0.894, 1},
{1, 1, 0.796, 0.78, 0.941, 0.941, 0.941, 0.592, 0.447, 0.565, 0.667, 0.737, 0.737, 0.667, 0.565, 0.451, 0.588, 0.941, 0.941, 0.941, 0.796, 0.808, 1, 1},
{1, 1, 0.996, 0.753, 0.788, 0.941, 0.941, 0.941, 0.941, 0.702, 0.584, 0.557, 0.553, 0.592, 0.698, 0.906, 0.941, 0.945, 0.941, 0.796, 0.769, 0.996, 1, 1},
{1, 1, 1, 1, 0.769, 0.745, 0.922, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.918, 0.741, 0.776, 1, 1, 0.996, 1},
{1, 0.996, 1, 1, 1, 0.851, 0.733, 0.773, 0.867, 0.945, 0.941, 0.941, 0.941, 0.941, 0.945, 0.867, 0.769, 0.725, 0.851, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 0.984, 0.843, 0.78, 0.761, 0.78, 0.796, 0.796, 0.784, 0.765, 0.78, 0.835, 0.984, 1, 1, 1, 1, 1, 1}
};

public TestPane() {
AsciiTableModel model = new AsciiTableModel();
model.setData(smily);

JTable table = new JTable(model);
table.setRowHeight(24);
Enumeration<TableColumn> columns = table.getColumnModel().getColumns();
while (columns.hasMoreElements()) {
TableColumn col = columns.nextElement();
col.setWidth(24);
col.setPreferredWidth(24);
col.setMinWidth(24);
col.setMaxWidth(24);
}
table.setRowHeight(24);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setDefaultRenderer(Object.class, new PaintTableCellRenderer());

setLayout(new BorderLayout());
add(new JScrollPane(table));
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

}

public class PaintTableCellRenderer extends DefaultTableCellRenderer {

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, "", isSelected, hasFocus, row, column);
if (value instanceof Double) {
double distance = (double) value;
int part = (int) (255 * distance);
Color color = new Color(part, part, part);
setBackground(color);
} else {
setBackground(Color.WHITE);
}
return this;
}

}

public class AsciiTableModel extends AbstractTableModel {

private double[][] data;

public AsciiTableModel() {
data = new double[24][24];
}

public void setData(double[][] value) {
data = value;
fireTableDataChanged();
}

@Override
public int getRowCount() {
return 24;
}

@Override
public int getColumnCount() {
return 24;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}

}

}

I had intended to provide a "sad" face which you could switch between, but my daughter wanted me to go paint with her, sorry ;)

Color a specific cell in a java JTable

Instead of:

if (isRowSelected(0) && isColumnSelected(0)) {
// ((JComponent) c).setBorder(new LineBorder(Color.red));
((JComponent) c).setBackground(Color.RED);
}

Use this piece of code:

c.setBackground((row == 0) && (column == 0) ? Color.RED : Color.WHITE);

Java - Change colors of some cells in JTable

what is happening is that only the cell with the last index of sponsorIndexArr is changing colors.

Your concept of a renderer is wrong. Your renderer has a loop which indicates you are attempting to render all the cells at one time. This is not how a renderer works

The same renderer is used for every cell. Every time a cell needs to be rendered the renderer is invoked. So if you have 10 rows the renderer is called 10 times and the state of the renderer will be updated 10 times to reflect the state of the cell.

I have an array of ints called sponsorIndexArr which contains the indices of the cells that I want to change the color

I would suggest that instead you should use a Set of Integers. Then your renderer will do a simple check to see if the row index is in the set which will then determine how the cell should be rendered.

The code might be something like:

@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

if (isSelected)
setBackground( table.getSelectionBackground() );
else if (yourSet.contains(row))
setBackground( Color.CYAN );
else
setBackground( table.getBackground() );

return this;
}

JTable Set Cell Color At Specific Value

when I select the row on the table, and I want method to only set color at specific column

Try with overridden prepareRenderer() method as suggested by @mKorbel.

sample code:

Object[] columnNames = { "A", "B", "C", "D" };
Object[][] data = {
{ "abc", new Double(850.503), 53, true },
{ "lmn", new Double(36.23254), 6, false },
{ "pqr", new Double(8.3), 7, false },
{ "xyz", new Double(246.0943), 23, true } };

JTable table = new JTable(data, columnNames) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
Component comp = super.prepareRenderer(renderer, row, col);
Object value = getModel().getValueAt(row, col);
if (getSelectedRow() == row) {
if (value.equals(false)) {
comp.setBackground(Color.red);
} else if (value.equals(true)) {
comp.setBackground(Color.green);
} else {
comp.setBackground(Color.white);
}
} else {
comp.setBackground(Color.white);
}
return comp;
}
};

When selected first row:

Sample Image

When selected second row.

Sample Image

Read more...



EDIT

As per your last comment

Is it possible to change color with out clicking (selecting) row on the table?

Yes just remove the check of selected row.

    Object value = getModel().getValueAt(row, col);
if (value.equals(false)) {
comp.setBackground(Color.red);
} else if (value.equals(true)) {
comp.setBackground(Color.green);
} else {
comp.setBackground(Color.white);
}

Sample Image

Can we use prepareRenderer component and custom cell Renderer together?

Don't try to do two things at once. You need to break the rendering down logically into two steps.

The renderer code is executed before the prepare renderer code. So:

  1. Frst you need to get the renderer working the way you want it to work.

  2. Then secondly you override the default rendering by highlighting the line containing the value 3.

The key point when changing the background in your renderer is that you must always reset it to the default value before doing any customization.

So in the rendering code you need your commented out lines:

// set color (ie. restore the default values)
cell.setBackground(new Color(0xFFFFFF));
cell.setForeground(new Color(0x000000));

Then in the prepareRenderer you don't need to reset the background, only change it when your condition is met:

// get rid of this, the default has already been set. 
// this code should only provide the override
//c.setBackground(getBackground());

Note: this will only work if you have a custom renderer on all the columns that resets the background to the default. Because we need to comment out the above statement, it is now the responsibility of each renderer to reset the background.



Related Topics



Leave a reply



Submit