Ioa*_* K. 6 verification swing input jtable verify
我正在尝试为JTable创建一个简单的输入验证器.我最终覆盖了方法:editingStopped().问题是该事件不包括有关已更新的单元格的信息.
这是我的"伪代码":
If (user finished editing a cell) {
Check if cell`s value is "1" or "0" or "-" (Karnaugh-Veitch)
If (check = false)
setValue (cell, "");
}
Run Code Online (Sandbox Code Playgroud)
我试过的第一个就是这里:
table.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
inputVerify (e.getColumn(), e.getFirstRow());
}
});
public void inputVerify (int column, int row) {
boolean verified = true;
String field = table.getValueAt(row, column).toString();
if (field != null && field.length() == 1) {
if ( !(field.charAt(0) == '0' || field.charAt(0) == '1' || field.charAt(0) == '-' ))
verified = false;
}
else {
verified = false;
}
if (!verified) {
table.getModel().setValueAt("", row, column);
java.awt.Toolkit.getDefaultToolkit().beep();
}
System.out.println ("Column = " + column + " Row = " + row + " Value = " + table.getValueAt(row, column) +" Verified = "+verified);
}
Run Code Online (Sandbox Code Playgroud)
但最终会出现:StackOverflow Exception.我想问题是:setValueAt(..)触发另一个tableChanged()事件,并且正在生成无限循环.
现在,我在这里尝试过:
table.getDefaultEditor(Object.class).addCellEditorListener(new CellEditorListener() {
// called when editing stops
public void editingStopped(ChangeEvent e) {
// print out the value in the TableCellEditor
System.out.println(((CellEditor) e.getSource()).getCellEditorValue().toString());
}
public void editingCanceled(ChangeEvent e) {
// whatever
}
});
Run Code Online (Sandbox Code Playgroud)
但正如你所看到的,我只能检索单元格的新值,而不是"坐标".我需要调用:setValueAt(..)方法,但我不知道如何获取单元格的坐标.
或者是否有更简单的方法来创建输入验证程序?
最好的问候Ioannis K.
kle*_*tra 12
第一:不能很好地支持JTable编辑的输入验证.几条评论
在所有这些(不完整,不幸;-)no-nos之后,有一点希望:最好的办法是实现一个自定义CellEditor,它在stopCellCellEditing中进行验证:如果新值无效,则返回false并可选择提供可视错误反馈.看看JTable.GenericEditor,了解如何做到这一点
| 归档时间: |
|
| 查看次数: |
6010 次 |
| 最近记录: |