javaswingjtablecellrenderer

Cell Renderer for JTable - coloured rows


I've been looking around for a solution to this and I can't make head nor tail from various places of how to get my table to do coloured rows without asking my own question.

From every place I've looked I gather I need to use a cell renderer but the problem is I don't know how to apply it to my own situation.

So I have a simple JTable with 3 columns and I simply want each row to be highlighted in either green, yellow or red depending on the value of a separate variable (not displayed in the table).

It seems like it should be really simple but I can't get how to do it. If it helps my table is defined like:

studentTableModel = new DefaultTableModel(new Object[]{"Name", "StudentNo", "Part"}, 0);
jt_studentTable = new JTable(studentTableModel);
jt_studentTable.getColumnModel().getColumn(2).setPreferredWidth(10);
studentTableModel.addRow(new Object[]{"(empty)", "(empty)", "(empty)"});
JScrollPane jsp_tableScroller = new JScrollPane(jt_studentTable);
jsp_tableScroller.setPreferredSize(new Dimension(200,190));
middleCentrePanel.add(jsp_tableScroller);

The rows in the table change depending of the selection of a combo box.

Thanks in advance.


Solution

  • JTable Cell Coloring

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableCellRenderer;
    
    
    public class RowRendering {
    
        private static Object[] columnName = {"Yes", "No"};
        private static Object[][] data = {
                {"Y", "N"},
                {"N", "Y"},
                {"Y", "N"}
        };
    
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
    
                    JFrame frame = new JFrame();
                    JTable table = new JTable(data, columnName);
                    table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer());
                    table.getColumnModel().getColumn(1).setCellRenderer(new CustomRenderer());
    
                    frame.add(new JScrollPane(table));
                    frame.setTitle("Rendering in JTable");
                    frame.pack();
                    frame.setVisible(true);
                }
            };
    
            EventQueue.invokeLater(r);
        }
    }
    
    
    class CustomRenderer extends DefaultTableCellRenderer 
    {
    private static final long serialVersionUID = 6703872492730589499L;
    
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
            if(row == 0){
                cellComponent.setBackground(Color.YELLOW);
            } else if ( row == 1){
                cellComponent.setBackground(Color.GRAY);
            } else {
                cellComponent.setBackground(Color.CYAN);
            }
            return cellComponent;
        }
    }