1

I have a jtable. I put checkbox in a column. But the check box are in multi selection mode,ie in my jtable there are 5 checkbox. i can select 5 of 5. I want to select only 1 at a time. How can I change it to single selection?

My code is like :

TableColumn colTable2 = jTable2.getColumnModel().getColumn(1);

colTable2.setCellEditor(new DefaultCellEditor(jCheckBox2));
colTable2.setCellRenderer(jTable2.getDefaultRenderer(Boolean.class)); 

Thanks in advance.

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
Rounak
  • 89
  • 1
  • 2
  • 10
  • a checkbox is either selected or not - nothing multiple that I can see. In other words: what do you _mean_? – kleopatra Jan 12 '12 at 07:12
  • in my jtable there are 5 checkbox. i can select 5 of 5. I want to select only 1 at a time. – Rounak Jan 12 '12 at 07:21
  • thanks for the clarification - you might consider to edit your question and add the additional info there – kleopatra Jan 12 '12 at 07:29
  • that logic belongs into your TableModel: in setValueAt which sets the corresponding value to true, check which is the current true and set that to false – kleopatra Jan 12 '12 at 07:33
  • @kleopatra:: Thanks a lot.... I have done it with setValueAt function. – Rounak Jan 12 '12 at 08:48

4 Answers4

1

Here is a TableModel that introduces a single selection check box column at the end of an existing table. You can install it like this

CheckBoxSelectionTableModel.register(table);

You can give it a try on the SimpleTableDemo - it will look like the following and keep the selected row in sync with the check box.

enter image description here

It only requires the existing table Model to be an instance of javax.swing.table.AbstractTableModel - which will be the case in > 80%.

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class CheckBoxSelectionTableModel implements TableModel, ListSelectionListener {

  protected final AbstractTableModel delegate;
  protected int selectedRow = -1;
  protected final ListSelectionModel selectionModel;

  public CheckBoxSelectionTableModel(AbstractTableModel delegate, ListSelectionModel selectionModel) {
    this.delegate = delegate;
    this.selectionModel = selectionModel;
    selectionModel.addListSelectionListener(this);
  }

  public static void register(JTable table) {
    table.setModel(new CheckBoxSelectionTableModel((AbstractTableModel)table.getModel(), table.getSelectionModel()));
  }

  protected boolean isCheckBoxCloumn(int columnIndex) {
    return columnIndex == getCheckBoxColumnIndex();
  }

  protected int getCheckBoxColumnIndex() {
    return delegate.getColumnCount();
  }

  // --------------------- delegate methods --------------------- \\

  public int getRowCount() {
    return delegate.getRowCount();
  }

  public int getColumnCount() {
    return getCheckBoxColumnIndex()+1;
  }

  public String getColumnName(int columnIndex) {
    return isCheckBoxCloumn(columnIndex) ? "" : delegate.getColumnName(columnIndex);
  }


  public Class<?> getColumnClass(int columnIndex) {
    return isCheckBoxCloumn(columnIndex) ? Boolean.class : delegate.getColumnClass(columnIndex);
  }

  public boolean isCellEditable(int rowIndex, int columnIndex) {
    return isCheckBoxCloumn(columnIndex) ? true : delegate.isCellEditable(rowIndex, columnIndex);
  }

  public Object getValueAt(int rowIndex, int columnIndex) {
    return  isCheckBoxCloumn(columnIndex) ?  rowIndex == selectedRow : delegate.getValueAt(rowIndex, columnIndex);
  }    

  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    if(isCheckBoxCloumn(columnIndex)) {
      int lastSelected = selectedRow;
      if((Boolean) aValue){
        selectedRow = rowIndex;
      } else {
        selectionModel.clearSelection();
        selectedRow = -1;
      }
      if(lastSelected > -1) {
        delegate.fireTableRowsUpdated(lastSelected, lastSelected);
      }
      delegate.fireTableRowsUpdated(rowIndex, rowIndex);
    } else {
      delegate.setValueAt(aValue, rowIndex, columnIndex);
    }
  }

  public void addTableModelListener(TableModelListener l) {
    delegate.addTableModelListener(l);
  }

  public void removeTableModelListener(TableModelListener l) {
    delegate.removeTableModelListener(l);
  }

  // --------------------- ListSelectionListener methods --------------------- \\

  @Override
  public void valueChanged(final ListSelectionEvent e) {
    if(e.getValueIsAdjusting()){
      return;
    }
    int index = selectionModel.getLeadSelectionIndex();
    boolean isSelected = selectionModel.isSelectedIndex(index);
    setValueAt(isSelected ,index , getCheckBoxColumnIndex());     
  }


}
culmat
  • 1,156
  • 11
  • 12
1

I was just looking something for this issue and I came to this solution... hope its helps someone.

private void MiTablaMouseClicked(java.awt.event.MouseEvent evt) {                                         
    for(int i=0; i<MiTabla.getRowCount(); i++){
        if(i==MiTabla.getSelectedRow()){
            MiTabla.setValueAt(true, MiTabla.getSelectedRow(), 0);
        }else{
            MiTabla.setValueAt(false, i, 0);
        }
    }

}
1

In your table model, when one of the 5 columns value is set to true, you should set the one which is currently true to false and fire a table model event for this column as well.

You should also consider using a radio button as renderer and editor, since it's the most appropriate component to represent a unique selection.

The alternative is to replace these 5 columns by a single one, and use a combo box as cell editor.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

I'd like to suggest you an alternative approach,

Have a look at JRadioButton, I guess this will be a better option.

Note , from How to Use Buttons, Check Boxes, and Radio Buttons

Check boxes are similar to radio buttons but their selection model is different, by convention. Any number of check boxes in a group — none, some, or all — can be selected. A group of radio buttons, on the other hand, can have only one button selected.

COD3BOY
  • 11,964
  • 1
  • 38
  • 56