How to add JCheckBox in JTable?

风格不统一 提交于 2019-11-27 15:14:39

Start with How to use tables.

Your table model needs several things.

  1. It needs to return Boolean.class from the getColumnClass method for the appropriate column. You will need to override this method.
  2. The method isCellEditable will need to return true for the table column you want to make editable (so the user can change the value of the column)
  3. You're table model will need to be capable of holding the value for the column
  4. Make sure you pass a valid value for the boolean column for the row, otherwise it will be null

Updated with simple example

import java.awt.EventQueue;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TableTest {

  public static void main(String[] args) {
    new TableTest();
  }

  public TableTest() {
    startUI();
  }

  public void startUI() {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
          ex.printStackTrace();
        }

        MyTableModel model = new MyTableModel();
        model.addRow(new Object[]{0, "Brian", false});
        model.addRow(new Object[]{1, "Ned", false});
        model.addRow(new Object[]{2, "John", false});
        model.addRow(new Object[]{3, "Drogo", false});
        JTable table = new JTable(model);

        JFrame frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    });
  }

  public class MyTableModel extends DefaultTableModel {

    public MyTableModel() {
      super(new String[]{"ID", "Name", "Present"}, 0);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
      Class clazz = String.class;
      switch (columnIndex) {
        case 0:
          clazz = Integer.class;
          break;
        case 2:
          clazz = Boolean.class;
          break;
      }
      return clazz;
    }

    @Override
    public boolean isCellEditable(int row, int column) {
      return column == 2;
    }

    @Override
    public void setValueAt(Object aValue, int row, int column) {
      if (aValue instanceof Boolean && column == 2) {
        System.out.println(aValue);
        Vector rowData = (Vector)getDataVector().get(row);
        rowData.set(2, (boolean)aValue);
        fireTableCellUpdated(row, column);
      }
    }

  }

}

Ps- I would HIGHLY recommend you avoid form editors until you have a better understanding of how Swing works - IMHO

Well if you add Boolean value as data into the table model, DefaultCellRendered will render it as checbox by itself, so where is the problem?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!