Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

color selected row in jtable by another color

I use this code to color the rows of ly jtable by differents colors :

table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
    {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            c.setBackground(row % 2 == 0 ? Color.WHITE : Color.LIGHT_GRAY);

            return c;
        }
    });

it works, now I want to color the row selected by the user to another color diferent than those above with :

table.setSelectionBackground(Color.RED);

but it does anything How can I achieve this ?

thank you in advance

like image 824
simonTifo Avatar asked Nov 20 '25 17:11

simonTifo


1 Answers

Your renderer is overriding the color change applied by the DefaultTableCellRenderer

Try something like...

Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (!isSelected) {
    c.setBackground(row % 2 == 0 ? Color.WHITE : Color.LIGHT_GRAY);
}

Instead

like image 146
MadProgrammer Avatar answered Nov 22 '25 06:11

MadProgrammer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!