JTable输入验证器 [英] JTable Input Verifier
问题描述
我正在尝试为JTable创建一个简单的输入验证器. 最后,我重写了方法:editingStopped(). 问题在于该事件不包含有关已更新单元的信息.
I am trying to create a simple Input Verifier for a JTable. I ended up with overriding the method: editingStopped(). The problem is that the event does not include informations about the cell that has been updated.
这是我的伪代码":
If (user finished editing a cell) {
Check if cell`s value is "1" or "0" or "-" (Karnaugh-Veitch)
If (check = false)
setValue (cell, "");
}
我第一次尝试的是这里:
The first I tried was this here:
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);
}
但是最终结果是:StackOverflow异常.我猜是问题所在:setValueAt(..)触发了另一个tableChanged()事件,并且正在生成一个无限循环.
But this ends up with an : StackOverflow Exception. I guess the problem is that: setValueAt(..) fires another tableChanged() event and an endless loop is being generated.
现在,我在这里尝试过
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
}
});
但是正如您所看到的,我只能获取单元格的新值,而不是坐标". 我需要调用:setValueAt(..)方法,但是我不知道如何获取单元格的坐标.
But as you can see I can just retrieve the new value of the cell, not the "coordinates". I need to call: setValueAt( .. ) method, but I dont know how to get the cell`s coordinates.
还是有一种更简单的方法来创建输入验证器?
Or is there a more simple way to create an input verifier??
最诚挚的问候 约阿尼斯·K.
Best regards Ioannis K.
推荐答案
首先:不完全支持JTable编辑的输入验证.一些评论
First: input validation on JTable editing is not well supported. A couple of comments
- TableModelListener中的tableChanged并不是进行验证的好地方,因为那时更改已经发生(模型将事实通知给其侦听器)
- 结果,无论您选择哪种验证(验证)方法挂钩,都永远不会回过头来讨论该模型,您最终将陷入无限循环(如您所见)
- 应用程序提供的CellEditorListeners相当无用,因为a)无法保证通知顺序(JTable可能尚未更新模型)b)编辑器的生命周期定义不明确
在所有这些(不完整的,不幸的是;-)不可以之后,有点希望:最好的选择是实现一个自定义CellEditor,它在stopCellCellEditing中进行验证:如果新值无效,则返回false并有选择地提供视觉错误反馈.查看JTable.GenericEditor,以了解如何实现
After all those (incomplete, unfortunately ;-) no-nos, a little hope: best bet is to implement a custom CellEditor which does the validation in stopCellCellEditing: if the new value isn't valid, return false and optionally provide a visual error feedback. Have a look at JTable.GenericEditor to get an idea of how that might be done
这篇关于JTable输入验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!