How to make only one checkbox selectable in JTable Column

后端 未结 3 1606

I am using DefaultTableModel as follows:

  DefaultTableModel model = new DefaultTableModel (COLUMNS, 0 )
  {
      @Override
      public boolean isCellEdita         


        
3条回答
  •  猫巷女王i
    2020-12-12 04:40

    You can create your own custom cell editor that joins all check boxes in a column in a ButtonGroup. here's how:

    public class VeryComplicatedCellEditor extends DefaultCellEditor {
        private ArrayList groups;
    
        public getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            JCheckBox checkBox = new JCheckBox();
            growToSize(column);
            groups.get(column).add(checkBox);
            return checkBox;
        }
    
        private growToSize(int size) {
            groups.ensureCapacity(size);
            while (groups.size() < size)
                groups.add(new ButtonGroup());
        }
    }
    

    There are some complications that come from the fact that we don't know how big the table is, which are mostly taken care of in the growToSize method.

    The way this works is by maintaining a list of ButtonGroups, one for each column. The editor component for each cell is added to the button group for its column.

提交回复
热议问题