JTable TableCellRenderer background with NimbusLookAndFeel color problem
Asked Answered
E

2

3

I'm using NimbusLookAndFeel. With this look and feel JTable's cell background are alternatively white and light grey (it depends on the row number). Now, I'm writing some custom cell renderer implementing TableCellRenderer. I need to set the background of these renderers according to the position of the cell in the JTable.

public class MyCellRenderer extends JLabel implements TableCellRenderer{


    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

                Color bgColor = //need to retrieve the right cell background color
                setBackground(bgColor);
        return this;
    }

}

How can I get such a Color value?

Extravagancy answered 4/7, 2011 at 13:2 Comment(1)
re-reading, I'm not entirely sure what you are asking: by default, Nimbus stripes the background of rows not of columns. What exactly is the problem you'r experiencing? BTW (nitpicking ...) in your snippet you don't extend TableCellRenderer, you implement it.Modular
M
8

Technically, you can access the color via the UIManager

   Color alternate = UIManager.getColor("Table.alternateRowColor");

Practically, I would not recommend to write renderers from scratch - there are many details to consider to get it right. Those details are handled f.i. in SwingX (biased me :-)

Expected you to do the logic yourself ;-). Here's a working snippet (assuming you want to color by row not by column, but changing that would be ... trivial):

    TableCellRenderer renderer = new TableCellRenderer() {

        JLabel label = new JLabel();

        @Override
        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus,
                int row, int column) {
            label.setOpaque(true);
            label.setText("" + value);
            Color alternate = UIManager.getColor("Table.alternateRowColor");
            if (row % 2 == 1) {
                label.setBackground(alternate);
            } else {
                label.setBackground(Color.WHITE);
            }
            return label;
        }

    };
    table.setDefaultRenderer(Object.class, renderer);
Modular answered 4/7, 2011 at 13:34 Comment(3)
I have no alternative. I am forced to write a custom cell renderer. I'll try your solution, thanks. But I don't understand how such a call can distinguish colors based on column number.Extravagancy
@Overbose - the logic of which color to apply to which cell was all left to you ;) Beware: there are states like selected, focused, editable, dragging .. and all combinations thereof to consider. What do you mean by "blanks"?Modular
you are right sorry. I meant row instead of column. I'm doing exactly what you suggest to me. I was missing setOpaque(true). Thanks.. your answer is correct. You saved me :)Extravagancy
K
-1

This should work perfectly :

public class MyRenderer extends DefaultTableCellRenderer { ... }
Kafiristan answered 17/5, 2016 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.