如何将Jtable中单元格的值设置为单击时输入的最后一个字符



我正在尝试这样做,当用户点击他们在JTable中编辑的单元格时,单元格的内容只设置为最后输入的字符。为了实现这一点,我有一个方法,它返回一个新的JTable,其中一个匿名类覆盖editingStop方法。现在这产生了两个错误:第一个是它不会在单元格中显示更新的字符串,第二个是lastChar变量被设置为点击单元格之前单元格中的最后一个字符

private JTable makeTable() {
String data[][] = { 
{ "Move Down", "hello" }};
String[] headers = { "Action", "Button" };
return new JTable(new DefaultTableModel(data, headers)) {
@Override
public boolean isCellEditable(int row, int column) {
return column == 1;
}
public void editingStopped(ChangeEvent e) {
String lastChar = getValueAt(getEditingRow(), 1).toString().substring(
getValueAt(getEditingRow(), 1).toString().length() - 1);
setValueAt(lastChar, getEditingRow(), 1);
System.out.println("Row " + (getEditingRow()) + " edited");
System.out.println("Cell set to:" + lastChar);
}
};
}

这可以通过调用超级方法来解决,因为它没有正确地离开单元格,从而产生问题。

public void editingStopped(ChangeEvent e) {
int row=getEditingRow();
System.out.println("Row " + (getEditingRow()) + " edited");
super.editingStopped(e);

String lastChar = getValueAt(row, 1).toString().substring(
getValueAt(row, 1).toString().length() - 1);
setValueAt(lastChar, row, 1);
System.out.println("Cell set to:" + lastChar);
}

最新更新