Java 单击复选框后,如何使JTable上的复选框不可编辑?
我创建了一个包含4列的Java 单击复选框后,如何使JTable上的复选框不可编辑?,java,swing,jtable,jcheckbox,Java,Swing,Jtable,Jcheckbox,我创建了一个包含4列的JTable,最后两列是JCheckBoxes。我想在单击第二列上的复选框后禁用第三列中的复选框,反之亦然 public class TableTest { public static void main(String[] args) { new TableTest(); } public TableTest() { startUI(); } public void startUI() { EventQueue.
JTable
,最后两列是JCheckBox
es。我想在单击第二列上的复选框后禁用第三列中的复选框,反之亦然
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();
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/survey";
Connection conn =
DriverManager.getConnection(url,"root","");
Statement stat = conn.createStatement();
ResultSet rslt=stat.executeQuery("SELECT * FROM
questions_edit");
while(rslt.next())
{
String d = rslt.getString("question_no.");
String e = rslt.getString("question");
model.addRow(new Object[]{d,e, false, false});
}
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
JTable table = new JTable();
table.setModel(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[]{"Question No.", "Question", "Satisfied", "Not
Satisfied"}, 0);
}
@Override
public Class<?> getColumnClass(int columnIndex) {
Class cls = String.class;
switch (columnIndex) {
case 0:
cls = Integer.class;
break;
case 2:
cls = Boolean.class;
break;
case 3:
cls = Boolean.class;
break;
}
return cls;
}
@Override
public boolean isCellEditable(int row, int column) {
switch (column) {
case 2: return true;
case 3: return true;
}
return false;
}
@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);
}
else if (aValue instanceof Boolean && column == 3) {
System.out.println(aValue);
Vector rowData = (Vector)getDataVector().get(row);
rowData.set(3, (boolean)aValue);
fireTableCellUpdated(row, column);
}
}
}
}
公共类TableTest{
公共静态void main(字符串[]args){
新表测试();
}
公共表格测试(){
startUI();
}
公共空间startUI(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化异常|
IllegalAccessException |不受支持的LookandFeelException ex){
例如printStackTrace();
}
MyTableModel模型=新的MyTableModel();
试试{
Class.forName(“com.mysql.jdbc.Driver”);
String url=“jdbc:mysql://localhost/survey";
连接连接=
getConnection(url,“根目录”,“根目录”);
语句stat=conn.createStatement();
ResultSet rslt=stat.executeQuery(“选择*FROM
问题(编辑);
while(rslt.next())
{
字符串d=rslt.getString(“问题号”);
字符串e=rslt.getString(“问题”);
addRow(新对象[]{d,e,false,false});
}
}捕获(SQLE异常){
e、 printStackTrace();
}catch(classnotfounde异常){
e、 printStackTrace();
}
JTable table=新的JTable();
表2.setModel(model);
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新的JScrollPane(表));
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类MyTableModel扩展了DefaultTableModel{
公共MyTableModel(){
超级(新字符串[]{“问题编号”,“问题”,“满意”,“未
满足“},0);
}
@凌驾
公共类getColumnClass(int columnIndex){
Class cls=String.Class;
开关(列索引){
案例0:
cls=Integer.class;
打破
案例2:
cls=Boolean.class;
打破
案例3:
cls=Boolean.class;
打破
}
返回cls;
}
@凌驾
公共布尔值可编辑(int行,int列){
开关(列){
案例2:返回true;
案例3:返回true;
}
返回false;
}
@凌驾
public void setValueAt(对象有效、整行、整列){
if(布尔值的aValue instanceof&&column==2){
系统输出打印(aValue);
Vector rowData=(Vector)getDataVector().get(行);
rowData.set(2,(布尔)aValue);
fireTableCellUpdated(行、列);
}
else if(布尔值和列的有效实例==3){
系统输出打印(aValue);
Vector rowData=(Vector)getDataVector().get(行);
rowData.set(3,(布尔)aValue);
fireTableCellUpdated(行、列);
}
}
}
}
这是我到目前为止所做的,但是第2列和第3列都是可编辑的。
请帮忙。多谢各位
但第2列和第3列都是可编辑的
好的,这就是您的isCellEditable(…)
方法的状态
如果不希望第3列可编辑,则需要修改代码。可能是这样的:
case 3:
Boolean column2 = (Boolean)getValueAt(row, 2)
return ! column2.booleanValue();
这将不会编译,也不是一个简单的问题。请更新您的问题。到目前为止您尝试了什么?您的问题到底是什么?我的问题是,单击jtable上第3列的复选框后,如何禁用第4列的复选框?您应该更新您的问题。“到目前为止你尝试了什么?”注意到,我已经更新了我的问题。到目前为止我已经完成了,但是第2列和第3列都是可编辑的。
isCellEditable
在选中/取消选中JCheckBox
时被调用。因为他想禁用“邻居”复选框,所以这不起作用,因为行
和列
指的是单击的复选框。@MoritzSchmidt他想禁用“邻居”复选框,-我知道,这就是为什么第3列的逻辑检查第2列的状态。