views:

1272

answers:

4

I am able to set one column to yellow but I am unable to set a row to yellow.

The following code does it for the column:

TableColumn col = mytable.getColumnModel().getColumn(0);

col.setCellRenderer(new MyTableCellRenderer());

How do I do it for a row please?

I have tried the tutorials and examples on the net but it always paints the whole table yellow rather than just one row.

Thanks

+2  A: 

what you need to do is generate a custom TableCellRenderer. see this tutorial for detail. your renderer will need to test the row index that is passed in and determine whether it is row 0 or not.

 public Component getTableCellRendererComponent(JTable table,
                                    Object value,
                                    boolean isSelected,
                                    boolean hasFocus,
                                    int row,
                                    int column) {
     if (row == 0) {
         setBackground(myBGColor)
     }
     ....
akf
+1  A: 

From "How to use tables" tutorial:

To specify a cell-specific renderer, you need to define a JTable subclass that overrides the getCellRenderer method. For example, the following code makes the first cell in the first column of the table use a custom renderer:

TableCellRenderer weirdRenderer = new WeirdRenderer();
table = new JTable(...) {
    public TableCellRenderer getCellRenderer(int row, int column) {
        if ((row == 0) && (column == 0)) {
            return weirdRenderer;
        }
        // else...
        return super.getCellRenderer(row, column);
    }
};

You can simply check for row == 0 then use your own renderer, else use the default.

macbirdie
It works now ..Thanks.
+1  A: 

I've typically solved this problem by implementing a decorator-style TableCellRenderer implementation that wraps another TableCellRenderer. That way you can retain type-specific renderers for each column, but wrap each of them in the decorator renderer responsible for row highlighting.

Here's an example I wrote that uses this approach to set the background of every alternate row to light grey.

public class AlternateRowRenderer implements TableCellRenderer {
    private final TableCellRenderer wrappedRenderer;

    public AlternateRowRenderer(TableCellRenderer wrappedRenderer, Color highlightColour) {
       this.wrappedRenderer = wrappedRenderer;
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean     isSelected, boolean hasFocus, int row, int column) {
        Component ret = wrappedRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        ret.setBackground(getTableBackgroundColour(table, value, isSelected, hasFocus, row, column));

        return ret;
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public static Color getTableBackgroundColour(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Color ret;

        if (row % 2 != 0) {
            ret = isSelected ? ColourUtil.mergeColours(LIGHT_GREY,     table.getSelectionBackground(), 0.75) : LIGHT_GREY;
        } else {
            ret = isSelected ? table.getSelectionBackground() : table.getBackground();
        }

        return ret;
    }
}
Adamski
A: 

You can set alternate colors for the jtable rows.. check out the following link in zybocodes http://www.zybonics.com/zybocodes/code/CodeViewForm.php?codeID=2

Chris