Totoro
Totoro

Reputation: 101

Jtable tablecellrenderer?

I have a java gui using dtable and I'm trying to highlight the last row added. In my GUI.java file, I have the dtable created using:

public JTable display = new JTable(model){
public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
    Component c = super.prepareRenderer(renderer, row, column);

    c.setBackground(Color.GREEN);

    return c;
}

};

In my main.java file, I create the gui class instance 'gu' with dtable instance 'display' and add rows using:

DefaultTableModel model = (DefaultTableModel) gu.display.getModel();
model.addRow(new Object[] {"col1","col2"});

All I want to do is highlight the last added row using the renderer. What would be the code to invoke it?

Upvotes: 1

Views: 269

Answers (1)

keuleJ
keuleJ

Reputation: 3496

What about this:

public JTable display = new JTable(model) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row,
            int column) {
        Component c = super.prepareRenderer(renderer, row, column);
        if (row == getColumnCount()) {
            c.setBackground(Color.GREEN);
        } else {
            if (row % 2 == 0) {
                c.setBackground(UIManager.getColor("Table.background"));
            } else {
                c.setBackground(UIManager
                        .getColor("Table.alternateRowColor"));
            }
        }
        return c;
    }

};

Upvotes: 1

Related Questions