简体   繁体   中英

How to set the colour of my 1st row in my JTable to yellow(or any colour)?

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

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)
     }
     ....

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.

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;
    }
}

For an easy way to set an alternate row colors, you can use SwingX to create JXTable ( the same as JTable + some cool extra features ) like :

myTabModel = new MyTableModel();
tab = new JXTable(myTabModel);

Color baseBackground = new Color(0,0,0,0);  // no color
Color alternateBackround = new Color(250,150,250,100);  // a fading purple color

tab.setHighlighters(HighlighterFactory.createAlternateStriping(baseBackground,alternateBackround);

note that the 1st row will have the baseBackground color

hope it helps

您可以为jtable行设置备用颜色。请查看zybocodes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM