Jtable checkbox single java swing selection

I have a jtable. I have checked a checkbox in a column. But the checkbox is in multiple selection mode, i.e. There are 5 checkboxes in my jtable. I can select 5 out of 5. I only want to select 1 at a time. How can I change it to one choice?

My code is like:

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

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

      

Thanks in advance.

+1


source to share


4 answers


In your table model, when one of the five column values ​​is set to true, you must set the value to true, which is currently true, and fire a table model event for that column.

You should also consider using a radio button as a renderer and editor, as it is the most appropriate component for presenting a unique selection.



An alternative is to replace these 5 columns with one, and use the combo box as the cell editor.

+1


source


I would like to offer you an alternative approach,

Have a look at JRadioButton , I think it would be the best option.



Note: How to use radio buttons, checkboxes and buttons

Checkboxes are similar to radio buttons, but their selection model is different, by convention. You can select any number of check boxes in a group - none, several, or all. On the other hand, a group of radio buttons can only select one button.

+1


source


Here is the TableModel that introduces one checkbox into the select box at the end of the existing table. You can install it like this:

CheckBoxSelectionTableModel.register(table);

      

You can try it SimpleTableDemo - it will look like this and keep the selected row in sync with the checkbox.

enter image description here

The existing Model table is required to be an instance of javax.swing.table.AbstractTableModel - this will be the case> 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());     
  }


}

      

+1


source


I was just looking for something for this problem and I came to this solution ... hope this 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);
        }
    }

}

      

0


source







All Articles