gpt4 book ai didi

java - 单击 JCheckBox 时禁用 JTable 内的 JComboBox

转载 作者:行者123 更新时间:2023-12-01 19:04:24 24 4
gpt4 key购买 nike

我有 JTable,它在两个不同的列中有一个 JCheckBox 和一个 JComoboBox。当我选择与该行对应的 JCheckBox 时,应禁用 JComboBox。请帮助我。

最佳答案

只需根据您的模型禁用单元格编辑即可。在您的 TableModel 中,重写/实现 isCellEditable() 方法以返回复选框的“值”。

虽然以下示例不是基于 JComboBox,但它说明了如何根据行开头的复选框的值禁用单元格的编辑:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class TestTable {

public JFrame f;
private JTable table;

public class TestTableModel extends DefaultTableModel {

public TestTableModel() {
super(new String[] { "Editable", "DATA" }, 3);
for (int i = 0; i < 3; i++) {
setValueAt(Boolean.TRUE, i, 0);
setValueAt(Double.valueOf(i), i, 1);
}
}

@Override
public boolean isCellEditable(int row, int column) {
if (column == 1) {
return (Boolean) getValueAt(row, 0);
}
return super.isCellEditable(row, column);
}

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

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
new TestTable().initUI();
}
});
}

protected void initUI() {
table = new JTable(new TestTableModel());
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 300);
f.setLocationRelativeTo(null);
f.add(new JScrollPane(table));
f.setVisible(true);
}

}

关于java - 单击 JCheckBox 时禁用 JTable 内的 JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10723956/

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