单击复选框后,如何使 JTable 上的复选框不可编辑?
How can I make a checkbox on JTable not editable after a checkbox is clicked?
我创建了一个包含 4 列的 JTable
,最后两列是 JCheckBox
。单击第二列上的复选框后,我想禁用第三列中的复选框,反之亦然。
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);
}
}
}
}
This is what I've done so far but both column 2 and 3 are editable.
Please help. Thank you.
but both column 2 and 3 are editable.
好吧,这就是你的 isCellEditable(...)
方法所说的。
如果您不想让第 3 列可编辑,那么您需要修改您的代码。可能是这样的:
case 3:
Boolean column2 = (Boolean)getValueAt(row, 2)
return ! column2.booleanValue();
我创建了一个包含 4 列的 JTable
,最后两列是 JCheckBox
。单击第二列上的复选框后,我想禁用第三列中的复选框,反之亦然。
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);
}
}
}
}
This is what I've done so far but both column 2 and 3 are editable. Please help. Thank you.
but both column 2 and 3 are editable.
好吧,这就是你的 isCellEditable(...)
方法所说的。
如果您不想让第 3 列可编辑,那么您需要修改您的代码。可能是这样的:
case 3:
Boolean column2 = (Boolean)getValueAt(row, 2)
return ! column2.booleanValue();