0

In my Jtable I have a column with boolean values displayed as Checkbox. I have added the Jtable TableCellRenderer and following is the code :

TableCellRenderer tableCellBoolean = new DefaultTableCellRenderer() {
        Boolean UserEnterValuse = new Boolean(false);

        public Component getTableCellRendererComponent(JTable table,
                Boolean value, boolean isSelected, boolean hasFocus,
                int row, int column) {
            if (value instanceof Boolean) {
                UserEnterValuse = Boolean.valueOf(value.toString());
                System.out.print(table.getCellRenderer(row, column));
                //InstallmentDate.get
                table.setValueAt(UserEnterValuse, row, column);

            }
            return super.getTableCellRendererComponent(table, value, isSelected,
                    hasFocus, row, column);
        }
    };

I have also added the setCellEditor but when i click on the Jtable cell then it show me the Checkbox and after selecting or changing the values in the cell it shows me true or false depending on selection type, but is does not show me Checkbox .

If i don't add TableCellRenderer and also when i set the values to Jtable cell it gives me error : Object can not cast to Boolean Type .

  • 1
    why you bothering with Renderer and Editor, this is implemented in APIs by default, for more info to read Oracle tutorial How to use Tables (parts about model, renderer and editor), for working code examples – mKorbel Mar 22 '16 at 07:32
  • 1
    Example of JTable with boolean checkBoxes: [Example](http://stackoverflow.com/questions/7391877/how-to-add-checkboxes-to-jtable-swing) – Сергей Дзюбка Mar 22 '16 at 07:34

1 Answers1

1

First of all you use the wrong method signature.

// wrong
public Component getTableCellRendererComponent(JTable table,
            Boolean value, boolean isSelected, boolean hasFocus,
            int row, int column) {

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

To get a checkbox displayed you need to extend check box in your renderer. Here is the correct renderer for Boolean values (it's a slightly modified renderer from JTable source).

public class BooleanRenderer extends JCheckBox implements TableCellRenderer {
    private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);

    public BooleanRenderer() {
        super();
        setHorizontalAlignment(JLabel.CENTER);
        setBorderPainted(true);
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
                                                   boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            super.setBackground(table.getSelectionBackground());
        }
        else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }
        setSelected((value != null && ((Boolean)value).booleanValue()));

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

        return this;
    }
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48