How to make only one checkbox selectable in JTable Column

后端 未结 3 1595

I am using DefaultTableModel as follows:

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


        
3条回答
  •  执笔经年
    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();
                }
            });
        }
    }
    

提交回复
热议问题