0

So I have this table:

table = new JTable();

Which models my model: table.setModel(model);

Inside I have a column called CheckColumn which has only CheckBoxes.

The thing is that if I check one box, it checks it and if I want to check another one, it removes the previous check. So it lets me check only one checkbox.

For the checkboxes I'm using the following:

table.getColumnModel().getColumn(15).setCellEditor(new CheckBoxCellEditor());
table.getColumnModel().getColumn(15).setCellRenderer(new CWCheckBoxRenderer());

From the Classes:

class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {

    private static final long serialVersionUID = 1L;
    protected JCheckBox checkBox;

    public CheckBoxCellEditor() {
        checkBox = new JCheckBox();
        checkBox.setHorizontalAlignment(SwingConstants.CENTER);
        checkBox.setBackground(Color.white);
    }

    public Component getTableCellEditorComponent(
            JTable table,
            Object value,
            boolean isSelected,
            int row,
            int column) {

        checkBox.setSelected(((Boolean) value).booleanValue());

        Component c = table.getDefaultRenderer(String.class).getTableCellRendererComponent(table, value, isSelected, false, row, column);
        if (c != null) {
            checkBox.setBackground(c.getBackground());
        }

        return checkBox;
    }
    public Object getCellEditorValue() {
        return Boolean.valueOf(checkBox.isSelected());
    }
}

And:

class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer {

    public CWCheckBoxRenderer() {
        super();
        setOpaque(true);
        setHorizontalAlignment(SwingConstants.CENTER);
    }

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

        if (value instanceof Boolean) {

            setSelected(((Boolean)value).booleanValue());
            setEnabled(table.isCellEditable(row, column));

            if (isSelected) {
                setBackground(table.getSelectionBackground());
                setForeground(table.getSelectionForeground());
            } else {

                setForeground(table.getForeground());
                setBackground(table.getBackground());
            }
        }
        else {
            setSelected(true);
            setEnabled(true);
            return null;
        }

        return this;
    }

}

I want to be able to check more than one row. Can someone tell me how can I do that? Is there something to be modified in the code or I need a new approach?

This is a summary of how the model looks like:

    private static final int CheckCol = 15;
    private List<Clients> clients;

    public boolean isCellEditable(int row, int col){
             if(col == 15){
                 return true;

             }
             return false;
         }


    public void setValueAt(Object value, int row, int col) {
            //if(col==15) (not sure what to do in here)

            //fireTableCellUpdated(row, col);
        }

    public Object getValueAt(int row, int col) {
            Clients tempClient = clients.get(row);

            switch (col) {

            case CheckCol:
                return false;  //my CheckColumn type is Boolean


            default:
                return false;

            }
}

I've tried everything I could think of to change the model...Can someone tell me how should the model look like? I need to get data from a mysql server and I don't know how to model the checkbox column.

Thanks in advance!

Alex
  • 357
  • 2
  • 15
  • Is there model storing the result? – MadProgrammer May 26 '15 at 21:46
  • What do you mean? The result of checking the box? – Alex May 26 '15 at 21:49
  • Yes. When the editor is dismissed, is the model notified (via the setValueAt method) and is it storing the value internally so it can return it via the getValueAt method – MadProgrammer May 26 '15 at 21:59
  • Sorry, I'm new to this. Should it be sth like: `Object status = table.getModel().getValueAt(row, 15); ` `table.getModel().setValueAt(value, row, 15);;` Or how? 15 is the no. of the checkbox column. – Alex May 26 '15 at 22:15
  • Nope, it should get done automatically. Hoe is you `TableModel` implemented – MadProgrammer May 26 '15 at 22:26
  • By the way...how can I put a new line in the comments section? – Alex May 26 '15 at 22:39
  • You don't, you put the code in the question instead. – user1803551 May 26 '15 at 22:51
  • Alright, I posted the model, thanks for the advice. Is something missing in the setValueAt() method? – Alex May 26 '15 at 22:57
  • Actually, your model is wrong, it should contain information which represents each cell (like a 2d array at the very simplest), this way, you know what data to return when asked and what data to store when required – MadProgrammer May 26 '15 at 23:04
  • Possible [duplicate](http://stackoverflow.com/q/4526779/230513). – trashgod May 27 '15 at 02:14
  • I've tried everything to change the model...can someone tell me how should the model look like? I need to get data from a mysql server and I don't know how to model the checkbox column. – Alex May 27 '15 at 08:33

0 Answers0