我有一个JTable,显示来自SQL数据库的行.该表相对较小(仅4列,最多1000行).
我想让用户有机会编辑表中的任何单元格,但要避免过多地限制它,以便他们必须使用编辑对话框(这使错误检查和验证容易得多,但不太直观)
我尝试了几种使用JTable的valueChanged方法控制编辑选择的方法,但是运气不佳.
我希望在编辑结束时对每一行进行编辑并将其写入数据库.我希望一旦单击一个单元格以开始对该行的编辑,就无法选择其他行,直到用户完成对该行的编辑为止(其他行显示为灰色).编辑每个单元格并按Enter后,编辑选择应跳到同一行的下一列.
谁能指出我该如何实现?
// Create table with database data
table = new JTable(new DefaultTableModel(data, columnNames)) {
public Class getColumnClass(int column) {
for (int row = 0; row < getRowCount(); row++) {
Object o = getValueAt(row, column);
if (o != null){
return o.getClass();
}
}
return Object.class;
}
@Override
public boolean isCellEditable(int row, int col){
return true;
}
@Override
public boolean editCellAt(int row, int column) {
boolean ans = super.editCellAt(row, column);
if (ans) {
Component editor = table.getEditorComponent();
editor.requestFocusInWindow();
}
return ans;
}
@Override
public void valueChanged(ListSelectionEvent source) {
super.valueChanged(source);
if (table!=null)
table.changeSelection(getSelectedRow(), getSelectedColumn()+1, false, false);
}
};
编辑-具有表指针的自定义单元格编辑器似乎是一个开始
public class ExchangeTableCellEditor extends AbstractCellEditor implements TableCellEditor {
private JTable table;
JComponent component = new JTextField();
public ExchangeTableCellEditor(JTable table) {
this.table = table;
}
public boolean stopCellEditing() {
boolean ans = super.stopCellEditing();
//now we want to increment the cell count
table.editCellAt(table.getSelectedRow(), table.getSelectedColumn()+1);
return ans;
}
@Override
public void cancelCellEditing() {
//do nothing... must accept cell changes
}
@Override
public Object getCellEditorValue() {
return ((JTextField)component).getText();
}
@Override
public Component getTableCellEditorComponent(JTable arg0, Object value,
boolean arg2, int arg3, int arg4) {
((JTextField)component).setText((String)value);
return component;
}
}
解决方法:
附录:我不熟悉您的片段中显示的方法.相反,如下所示,向您的模型注册一个TableModelListener,并使用保证的粒度更新数据库.另请参见How to Use Tables: Listening for Data Changes.
附录:@kleopatra关于您的TableCellEditor是正确的.通知侦听器的一种便捷方法是调用超级实现,如here所示.请注意,委托调用fireEditingStopped().
/** @see https://stackoverflow.com/questions/9155596 */
public class NewJavaGUI extends JPanel {
private final JTable table;
public NewJavaGUI() {
String[] colNames = {"C1", "C2", "C3"};
DefaultTableModel model = new DefaultTableModel(colNames, 0) {
@Override
public boolean isCellEditable(int row, int col) {
// return your actual criteria
return true;
}
@Override
public Class getColumnClass(int col) {
// return your actual type tokens
return getValueAt(0, col).getClass();
}
};
// Add data; note auto-boxing
model.addRow(new Object[]{"A1", "A2", 42});
model.addRow(new Object[]{"B1", "B2", 42d});
model.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
// DML as indicated
}
});
table = new JTable(model);
this.add(table);
}
private void display() {
JFrame f = new JFrame("NewJavaGUI");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new NewJavaGUI().display();
}
});
}
}
标签:swing,jtable,java
来源: https://codeday.me/bug/20191201/2083328.html