jtable set column background color code example

Example: jtable set column background color

table.getColumn("Column title").setCellRenderer(
                new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table,
                    Object value, boolean isSelected, boolean hasFocus, int row, int col) {

                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);

                Float quantityRemaining = (Float) table.getModel().getValueAt(row, col);
                    if (quantityRemaining < 1) {
                        setBackground(table.getBackground());
                        setForeground(Color.RED);
                        setToolTipText("This item needs immediate restocking!!!");
                    } else {
                        setBackground(table.getBackground());
                        setForeground(table.getForeground());
                        setToolTipText("Resonable stock quantities");
                    }
                return table;
            }
        });
    }

Tags:

Misc Example