JAVA - JTable 中的验证

发布于 2024-07-26 01:35:36 字数 85 浏览 1 评论 0原文

我试图在输入数据后立即验证 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌生 2024-08-02 01:35:37

您应该实现一个 TableCellEditor 并在 stopCellEditing() 方法中执行验证。 如果验证失败,此方法应返回 false。 来自 Javadoc:

“告诉编辑器停止编辑并接受任何部分编辑的值作为编辑器的值。如果编辑未停止,则编辑器返回 false;这对于验证且不能接受无效条目的编辑器非常有用”。”

查看 JTable 中定义的 GenericEditor 类作为示例。

另一件值得关注的事情是:您始终可以构造一个带有 JFormattedTextField 作为参数的 DefaultCellEditor ,并向文本字段添加一个 InputVerifier 以防止防止提交无效数据的输入。

You should implement a TableCellEditor and perform your validation within the stopCellEditing() method. If validation fails this method should return false. 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 a JFormattedTextField as a parameter and add an InputVerifier to the text field to prevent entry of invalid data from being committed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文