JAVA - JTable 中的验证
我试图在输入数据后立即验证 JTable
中的单元格。 我想确保在代码栏中输入了正确的代码。 有人可以让我知道该怎么做吗?
I am trying to validate a cell in the JTable
as soon as the data is entered.
I want to make sure that say the correct code is entered in the code column.
Could anyone let me know how to do this please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该实现一个
TableCellEditor
并在stopCellEditing()
方法中执行验证。 如果验证失败,此方法应返回false
。 来自 Javadoc:“告诉编辑器停止编辑并接受任何部分编辑的值作为编辑器的值。如果编辑未停止,则编辑器返回 false;这对于验证且不能接受无效条目的编辑器非常有用”。”
查看
JTable
中定义的 GenericEditor 类作为示例。另一件值得关注的事情是:您始终可以构造一个带有
JFormattedTextField
作为参数的DefaultCellEditor
,并向文本字段添加一个InputVerifier
以防止防止提交无效数据的输入。You should implement a
TableCellEditor
and perform your validation within thestopCellEditing()
method. If validation fails this method should returnfalse
. From the Javadoc:"Tells the editor to stop editing and accept any partially edited value as the value of the editor. The editor returns false if editing was not stopped; this is useful for editors that validate and can not accept invalid entries."
Take a look at the GenericEditor class defined within
JTable
for an example of this.One other thing worth looking at: You could always construct a
DefaultCellEditor
with aJFormattedTextField
as a parameter and add anInputVerifier
to the text field to prevent entry of invalid data from being committed.