我正在使用netbeans中的java swing。我的表单中有一个jtable,它将填充mysql数据库中的值。表会根据用户选择的表动态更改。我已添加了一列复选框结束。现在无论显示什么表,我只通过覆盖可以编辑的最后一列是单元格可编辑的函数。
所以现在复选框列是可编辑的。我想在检查对应于该行的复选框时,使其余几列可以编辑,并且我想在复选框为止后使该行不可编辑签出!!
我需要帮助才能完成这项任务。如果需要任何其他细节,请询问。
提前致谢。
答案 0 :(得分:1)
Kleopatra当然是对的 - 不需要听众。您需要做的就是让模型检查感兴趣行的最后一列所保存的布尔值,并将其作为isCellEditable()
值返回。
例如:
@SuppressWarnings("serial")
class MyTableModel extends DefaultTableModel {
public static final String SELECT = "select";
String tablename;
public MyTableModel(Object[][] rowData, Object[] columnNames, String tableName) {
super(rowData, columnNames);
this.tablename = tableName;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
if (getColumnName(columnIndex).equalsIgnoreCase(SELECT)) {
return Boolean.class;
}
return super.getColumnClass(columnIndex);
}
@Override
public boolean isCellEditable(int row, int col) {
if (col == getColumnCount() - 1)
return true;
else
return ((Boolean) getValueAt(row, getColumnCount() - 1)).booleanValue();
}
}
可以测试:
import javax.swing.*;
import javax.swing.table.*;
public class TestBoolTableModel {
public static final Integer[][] DATA = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 },
{ 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
public static final String[] COL_NAMES = { "One", "Two", "Three", "Four",
MyTableModel.SELECT };
private static void createAndShowGui() {
Object[][] rowData = new Object[DATA.length][];
for (int i = 0; i < rowData.length; i++) {
rowData[i] = new Object[DATA[i].length + 1];
for (int j = 0; j < DATA[i].length; j++) {
rowData[i][j] = DATA[i][j];
}
rowData[i][DATA[i].length] = Boolean.FALSE;
}
MyTableModel tableModel = new MyTableModel(rowData, COL_NAMES, "My Table");
JTable table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
JFrame frame = new JFrame("TestBoolTableModel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}