JTable复选框仅在选中复选框时更改

时间:2015-07-10 17:58:20

标签: java swing jtable

我有一个有3列的JTable,前两列是复选框。我通过覆盖DefaultTableModel使它们成为复选框。像这样:

tableData = new DefaultTableModel(columnNames, 0) {
        /**
        private static final long serialVersionUID = 1L;

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndex == 0 || columnIndex == 1)
                return Boolean.class;
            return super.getColumnClass(columnIndex);
        }
}

我的问题是,如果您单击单元格中的任意位置,则复选框将选中或取消选中。我希望它只在单击框本身时才会这样做。

我确实看到了其他选项,但他们需要一种不同的方法来创建表格中的复选框。有没有一种简单的方法来实现这一点,保持我已经开始的方法?

1 个答案:

答案 0 :(得分:2)

或许在JCheckBox的中心设置JPanel作为JTable的默认Boolean TableCellEditor可能更简单:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class CheckBoxPanelCellTest {
  private JComponent makeUI() {
    String[] columnNames = {"String", "Boolean"};
    Object[][] data = {
      {"AAA", true}, {"bbb", false}
    };
    DefaultTableModel model = new DefaultTableModel(data, columnNames) {
      @Override public Class<?> getColumnClass(int column) {
        if (column == 1) {
          return Boolean.class;
        }
        return super.getColumnClass(column);
      }
      @Override public boolean isCellEditable(int row, int column) {
        return column == 1;
      }
    };
    JTable table = new JTable(model) {
      @Override public void updateUI() {
        setDefaultEditor(Boolean.class, null);
        super.updateUI();
        setDefaultEditor(Boolean.class, new CheckBoxPanelEditor());
      }
    };
    table.setRowHeight(24);
    table.setRowSelectionAllowed(true);
    table.setShowVerticalLines(false);
    table.setIntercellSpacing(new Dimension(0, 1));
    table.setFocusable(false);
    return new JScrollPane(table);
  }
  public static void main(String... args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new CheckBoxPanelCellTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

class CheckBoxPanelEditor extends AbstractCellEditor implements TableCellEditor {
  private final JPanel p = new JPanel(new GridBagLayout());
  private final JCheckBox checkBox = new JCheckBox();
  public CheckBoxPanelEditor() {
    super();
    checkBox.setOpaque(false);
    checkBox.addActionListener(new ActionListener() {
      @Override public void actionPerformed(ActionEvent e) {
        fireEditingStopped();
      }
    });
    p.add(checkBox);
    p.setBorder(BorderFactory.createEmptyBorder(0, 1, 0, 0));
  }
  @Override public Component getTableCellEditorComponent(
      JTable table, Object value, boolean isSelected, int row, int column) {
    checkBox.setSelected(Objects.equals(value, Boolean.TRUE));
    p.setBackground(table.getSelectionBackground());
    return p;
  }
  @Override public Object getCellEditorValue() {
    return checkBox.isSelected();
  }
}