Coloring jTable row
16,166
Solution 1
Here is an example on how you can combine both column colors and row color. You basically perform tests in the TableCellRenderer to see if the background should be of one color or another.
import java.awt.Color;
import java.awt.Component;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class TestTable {
public class MyTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setBackground(null);
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setText(String.valueOf(value));
boolean interestingRow = row % 5 == 2;
boolean secondColumn = column == 1;
if (interestingRow && secondColumn) {
setBackground(Color.ORANGE);
} else if (interestingRow) {
setBackground(Color.YELLOW);
} else if (secondColumn) {
setBackground(Color.RED);
}
return this;
}
}
private JFrame f;
private JTable table;
protected void initUI() {
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
Vector<String> columNames = new Vector<String>();
columNames.add("Col 0");
columNames.add("Col 1");
columNames.add("Col 2");
for (int i = 0; i < 20; i++) {
Vector<Object> v = new Vector<Object>();
v.add(i % 3 == 0 ? "Hello" : "World");
v.add("Some data in row " + (i + 1));
v.add("Some other data in row " + (i + 1));
data.add(v);
}
table = new JTable(new DefaultTableModel(data, columNames));
Enumeration<TableColumn> en = table.getColumnModel().getColumns();
while (en.hasMoreElements()) {
TableColumn tc = en.nextElement();
tc.setCellRenderer(new MyTableCellRenderer());
}
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.add(new JScrollPane(table));
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestTable().initUI();
}
});
}
}
Solution 2
As you want to alter entire rows, irrespective of column class, consider overriding prepareRenderer()
, as discussed here. The TableCellRenderer
and prepareRenderer()
approaches are contrasted here.
Comments
-
Luna almost 2 years
I want to color specific rows in jTable..i did it for columns by using this code,
private class CustomCellRenderer extends DefaultTableCellRenderer { /* (non-Javadoc) * @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int) */ @Override public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) { Component rendererComp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,row, column); //Set foreground color // rendererComp.setForeground(Color.red); //Set background color rendererComp .setBackground(Color.pink); return rendererComp ; } }
And i call the above code using,
jTable1.getColumnModel().getColumn(3).setCellRenderer(new CustomCellRenderer());
But i want to do the same for rows in jTable..There's no getColumnModel() or getColumn() in the case of rows..So what's the alternate way for doing that? I am doing it in Netbeans by using Java Swing..
-
Guillaume Polet about 12 yearsI don't see how this is dirty. On the contrary, using a JTable to display TableData is actually a good practice. Too often people use complex layouts and panels to display a TableData.
-
Vedant Agarwala about 12 yearsusing JTable is fine but u should implement the Interface TableCellRenderer rather than inheriting the whole class
-
Guillaume Polet about 12 yearsWhat would be the point of that? Using the DefaultTableCellRenderer you also automatically get appropriate L&F-dependent colors (background selection color is different across different L&F/Platforms). Unless the rendering is actually not a JLabel but something more complex, I don't see why not reuse what's already there.
-
Luna about 12 yearsfor(int i=0;i<serialNumber;i++){ if((jTable1.getValueAt(i,1).toString()).equals(BidNumber)){ } } I need to call that from this if condition by passing 'i' value as row and 1 as column..can you please simplify the code for that?
-
Guillaume Polet about 12 years@Tickua I am not sure I understand your comment. In the method
getTableCellRendereComponent
, you have access to all the information you may need: the JTable, the current value of the cell, and the position of the cell in the table (row and column), plus wheter the current row is selected and if the current cell is focused. From the table you also get access to your TableModel (table.getModel()). I think that it should be quite easy from there to compute whatever condition you want and set the appropriate background color. -
Luna about 12 yearsThat I know..But my question is how can i invoke the method getTableCellRendereComponent() by specifying my i'th row and 1st column..I tried in this way also..final CustomCellRenderer renderer = new CustomCellRenderer(); , renderer.getTableCellRendererComponent(jTable1, 1, true,true, i, 1);
-
Guillaume Polet about 12 years@Tickua You don't call the TableCellRenderer yourself, you let the JTable do that for you. It will call you for all your rows automatically the first time it needs to be rendered, and then by firing appropriate TableModel events, it will eventually reinvoke your TableCellRenderer. If you still have issues with that, consider reading this tutorial or post another question on SO.
-
Luna almost 12 yearsRow coloring is working according to your example,but one problem is there..Can you help me