0

I have a JTable on a Netbeans 8.2 project which shows the data of a list a class (let's call it Client) and a checkbox on the first column of each line. I've setup this way changing the data type of the column in the object's properties menu. How can I not show this checkbox in case that a client's attribute is false?

1
  • You can always not show a certain component by calling setVisible(false) on it. Jun 1, 2020 at 3:07

1 Answer 1

1

I assume the first column is not editable. If you want to hide the JCheckBox when cell value is false, you can use a customer cell renderer:

private class MyCellRenderer extends DefaultTableCellRenderer
{

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col)
    {
        Component c;
        if (((Boolean)value).equals(true))
        {
            // Use the default renderer for Boolean which is JCheckBox based
            c = myTable.getDefaultRenderer(table.getColumnClass(col)).getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
        } else
        {
            // Use the standard default renderer which is a JLabel
            c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
            if (c instanceof JLabel)
            {
                ((JLabel) c).setText(null);
            }
        }
        return c;
    }
}

And apply it on the first boolean column:

myTable.getColumnModel().getColumn(0).setCellRenderer(new MyCellRenderer());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.