gpt4 book ai didi

java - 使用 setValueAt 重新创建互斥复选框

转载 作者:搜寻专家 更新时间:2023-11-01 01:18:29 25 4
gpt4 key购买 nike

我有一个使用自定义 DefaultTableModel 的 JTable,它在最后一列中有一些 boolean 值(显示为复选框)。

当我添加一个 MouseListener 来检索被点击的值时,似乎不再发生勾选框的切换。

// As soon as this is used in the component
// that is using the JTable, the toggling stops
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
int col = table.getSelectedColumn();
int row = table.getSelectedRow();

Object o = table.getModel().getValueAt(row, col);

我假设事件正在被监听器使用。我可以在 MouseListener 代码中添加什么来恢复切换行为?

编辑:

糟糕,看来问题出在我的覆盖上:

@Override
public void setValueAt(Object aValue, int row, int column) {

// Attempt at mutually exclusive checkboxes
if( column == 2 ){ // Starts at 0. Seek an alternative solution to avoid hardcoding?
// Algorithm: cycle through List to set other Booleans to false

// Uses entities. Is there another way of getting the number of rows from model?
List<MyEntity> myEntities = this.getDatas();

for( int i = 0; i < myEntities.size(); i++ ){
if( i != row ){
// Make sure this calls parent
super.setValueAt( false , i, 2);
}
}

} else {
super.setValueAt(aValue, row, column); // Call parent class
}

}

最佳答案

不要添加您自己的 MouseListener。而是覆盖 TableModel 中的 setValueAt() 以查看默认编辑器为 Boolean.class 设置的新值。

附录:这是一个 sscce .为了方便起见,它只是清除 CHECK_COL 中的所有条目,设置新值并相应地调节按钮。

CheckOne image

import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
* @see http://stackoverflow.com/questions/7920068
* @see http://stackoverflow.com/questions/4526779
*/
public class CheckOne extends JPanel {

private static final int CHECK_COL = 1;
private static final Object[][] DATA = {
{"One", Boolean.FALSE}, {"Two", Boolean.FALSE},
{"Three", Boolean.FALSE}, {"Four", Boolean.FALSE},
{"Five", Boolean.FALSE}, {"Six", Boolean.FALSE},
{"Seven", Boolean.FALSE}, {"Eight", Boolean.FALSE},
{"Nine", Boolean.FALSE}, {"Ten", Boolean.FALSE}};
private static final String[] COLUMNS = {"Number", "CheckBox"};
private DataModel dataModel = new DataModel(DATA, COLUMNS);
private JTable table = new JTable(dataModel);
private ControlPanel cp = new ControlPanel();

public CheckOne() {
super(new BorderLayout());
this.add(new JScrollPane(table));
this.add(cp, BorderLayout.SOUTH);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setPreferredScrollableViewportSize(new Dimension(250, 175));
}

private class DataModel extends DefaultTableModel {

public DataModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
}

@Override
public void setValueAt(Object aValue, int row, int col) {
if (col == CHECK_COL) {
for (int r = 0; r < getRowCount(); r++) {
super.setValueAt(false, r, CHECK_COL);
}
}
super.setValueAt(aValue, row, col);
cp.button.setEnabled(any());
}

private boolean any() {
boolean result = false;
for (int r = 0; r < getRowCount(); r++) {
Boolean b = (Boolean) getValueAt(r, CHECK_COL);
result |= b;
}
return result;
}

@Override
public Class<?> getColumnClass(int col) {
if (col == CHECK_COL) {
return getValueAt(0, CHECK_COL).getClass();
}
return super.getColumnClass(col);
}

@Override
public boolean isCellEditable(int row, int col) {
return col == CHECK_COL;
}
}

private class ControlPanel extends JPanel {

JButton button = new JButton("Button");

public ControlPanel() {
button.setEnabled(false);
this.add(new JLabel("Selection:"));
this.add(button);
}
}

private static void createAndShowUI() {
JFrame frame = new JFrame("CheckOne");
frame.add(new CheckOne());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
createAndShowUI();
}
});
}
}

关于java - 使用 setValueAt 重新创建互斥复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7920068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com