How to make only one checkbox that can be selected in the JTable column

I am using DefaultTableModel as follows:

  DefaultTableModel model = new DefaultTableModel (COLUMNS, 0 )
  {
      @Override
      public boolean isCellEditable(int row, int column)
      {
          return (getColumnName(column).equals("Selected"));
      }

      public Class getColumnClass(int columnIndex)
      {
          if(getColumnName(columnIndex).equals("Selected"))
              return Boolean.class;
          return super.getColumnClass(columnIndex);
      }     
  };

Now I want to make only one checkbox, which can be selected in the "Selected" column. How can this be done. I also tried the following method but did not work.

 public void fireTableCellUpdated(int row,int column)
 {
     if(getColumnName(column).equals("Selected"))
     {
         for(int i = 0; i<getRowCount() && i!=row;i++)
            setValueAt(Boolean.FALSE, row, column);
     }
 }
+1
source share
3 answers
  • @eatSleepCode wrote @mKorbel if you can give some sample code to implement the setValueAt method.

  • code for (using OP) DefaultTableModel,

  • for code based AbstractTableModelit is required to store the code for the notifier fireTableCellUpdated(rowIndex, columnIndex); because / otherwise nothing will be repainted in JTables view,

  • , ( ) AbstractTableModel (99pct )

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();
            }
        });
    }
}
+1

, setValueAt() fireTableCellUpdated().

, , false.

0

You can create your own cell editor that combines all the checkboxes in column c ButtonGroup. here as:

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 difficulties associated with the fact that we do not know how large the table is, which is mainly taken care of in the method growToSize.

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

-1
source

Source: https://habr.com/ru/post/1611310/


All Articles