简体   繁体   中英

JTable checkbox only change when checkbox is selected

I have a JTable that has 3 columns, the first two columns are check boxes. I have made them check boxes through overriding the DefaultTableModel. Like this:

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);
        }
}

My problem is that if you click anywhere in the cell the checkbox will check or uncheck. I would like it so it only does it when the box itself is clicked.

I did see other options for doing this, but they required a different method for created the checkbox in the table. Is there a simple way of implementing this, keeping the method I have already started on?

Perhaps setting a JCheckBox in the center of a JPanel as the JTable 's default Boolean TableCellEditor might be simpler:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM