How to make only one checkbox selectable in JTable Column

后端 未结 3 1596

I am using DefaultTableModel as follows:

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


        
相关标签:
3条回答
  • 2020-12-12 04:17

    You get an stack overflow exception because setValueAt() method triggers fireTableCellUpdated() method once again and again.

    Instead, try using a table listener which would listen to check box's value change and would set all other check boxes' value to false.

    0 讨论(0)
  • 2020-12-12 04:34
    • @eatSleepCode wrote @mKorbel can you please give example code for implementing setValueAt method.

    • code for (OP used) DefaultTableModel,

    • for code based on AbstractTableModel is required to hold code ordering for notifier fireTableCellUpdated(rowIndex, columnIndex);, because/otherwise nothing will be repainted in JTables view,

    • there are a few important differencies betweens those two models and its notifiers, and (my view) there isn't reason to bothering with and to use AbstractTableModel for basic stuff (99pct of table models)

    enter image description here . . . . enter image description here . . . . enter image description here

    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;
    
    public class TableRolloverDemo {
    
        private JFrame frame = new JFrame("TableRolloverDemo");
        private JTable table = new JTable();
        private String[] columnNames = new String[]{"Column"};
        private Object[][] data = new Object[][]{{false}, {false}, {true}, {true},
            {false}, {false}, {true}, {true}, {false}, {false}, {true}, {true}};
    
        public TableRolloverDemo() {
            final DefaultTableModel model = new DefaultTableModel(data, columnNames) {
                private boolean ImInLoop = false;
    
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return Boolean.class;
                }
    
                @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return true;
                }
    
                @Override
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
                    if (columnIndex == 0) {
                        if (!ImInLoop) {
                            ImInLoop = true;
                            Boolean bol = (Boolean) aValue;
                            super.setValueAt(aValue, rowIndex, columnIndex);
                            for (int i = 0; i < this.getRowCount(); i++) {
                                if (i != rowIndex) {
                                    super.setValueAt(!bol, i, columnIndex);
                                }
                            }
                            ImInLoop = false;
                        }
                    } else {
                        super.setValueAt(aValue, rowIndex, columnIndex);
                    }
                }
            };
            table.setModel(model);
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new JScrollPane(table));
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    TableRolloverDemo tableRolloverDemo = new TableRolloverDemo();
                }
            });
        }
    }
    
    0 讨论(0)
  • 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<ButtonGroup> 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.

    0 讨论(0)
提交回复
热议问题