samedi 14 octobre 2023

Java Table doesn't get rendered as expected

I did some attempts with a JTable. I want the table to contain some checkboxes and having certain background colors.

Everything is working fine from row 2 towards the end of the table. But unfortunately row 1 gets not rendered as expected. I’m wondering because row 1 is fine, as soon, as I resize the whole JDialog, which contains my table.

Adding data and the renderer to the table:

databaseTable.setModel(new DefaultTableModel(data, head));
databaseTable.getColumnModel().getColumn(0).setMinWidth(0);
databaseTable.getColumnModel().getColumn(0).setMaxWidth(0);
databaseTable.getColumnModel().getColumn(0).setWidth(0);
databaseTable.setDefaultRenderer(Object.class, new MyTableCellRenderer());

The Class MyTableCellRenderer

public class MyTableCellRenderer extends DefaultTableCellRenderer {

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

        final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);


        if (isSelected)
        {
            c.setBackground(RowColors.selectedRow.getColor());
        }
        else
        {
            c.setBackground(row % 2 == 0 ? RowColors.evenRow.getColor() : RowColors.oddRow.getColor());
        }

        List<Integer> values = Lists.newList(2, 3,4,6,8);
        if (values.contains(col))
        {
            MyBooleanRenderer br = new MyBooleanRenderer();
            table.getColumnModel().getColumn(col).setCellRenderer(br);
            table.getColumnModel().getColumn(col).setCellEditor(table.getDefaultEditor(Boolean.class));
            c.setBackground(row % 2 == 0 ? RowColors.evenRow.getColor() : RowColors.oddRow.getColor());
        }

        return c;
    }

and finally the Class MyBooleanRenderer:

public  class MyBooleanRenderer extends JCheckBox implements TableCellRenderer, UIResource {

        private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);

        public MyBooleanRenderer() {
            super();
            setHorizontalAlignment(JLabel.CENTER);
            setBorderPainted(true);
            setOpaque(true);

        }

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

                super.setBackground(table.getSelectionBackground());
            } else {
                setBackground(row % 2 == 0 ? RowColors.evenRow.getColor() : RowColors.oddRow.getColor());
            }
            setSelected((value != null && ((Boolean) value).booleanValue()));

            if (hasFocus) {
                setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
            } else {
                setBorder(noFocusBorder);
            }

            return this;
        }
    }

Table with incorrect rendering in row 1




Aucun commentaire:

Enregistrer un commentaire