问题标签 [inputverifier]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
63 浏览

java - java 扩展了 javax.swing.jframe 和 inputverifier

我需要扩展 javax.swing.jframe 和 inputverifier。我可以恢复这个问题吗??? 我尝试使用更多类,但这不起作用。我可以使用内部类吗?

}

0 投票
1 回答
99 浏览

java - 在另一个类中使用抽象类 inputverifier java

我有两节课

我想在 Finestra 中使用 Verifca 类。我没有扩展它,因为有 javax.swing.JFrame。我可以使用 Verifica 吗?它是唯一的抽象方法验证的问题。

我试试这个,但不起作用

0 投票
1 回答
686 浏览

java - VerifyInputWhenFocusTarget property has no effect

I am attempting to validate the text field user inputs using javax.swing.InputVerifier and the input validation works as expected but I have a problem regarding VerifyInputWhenFocusTarget property.

I've made a label to show the status and overridden verify() and shouldYieldFocus() methods of InputVerifier subclass and that works fine.

The next step I wanted to do was to set the VerifyInputWhenFocusTarget of the button so it wouldn't get the focus in case the validation of the current focus owner was false, but I did not notice any effect of setting the VerifyInputWhenFocusTarget property to true and the button could be pressed even when the validation of the current focus owner was false.

Maybe I do not understand the docs - I thought setting the VerifyInputWhenFocusTarget property of the button to true would prevent the button to get the focus when clicked under the circumstances of the false validation of the text field. Furthermore, I (mis)understood if the button couldn't get the focus then its actionPerformed() method wouldn't be called.

However the button can be clicked and its actionPerformed() method gets executed nevertheless of the false validation of the text field(s) 'guarded' by javax.swing.InputVerifier.

Here is the stripped code:

And here is the screenshot of the test application:

InputVerifier Test screenshot

As can be seen, there are two buttons. One of them has the VerifyInputWhenFocusTarget property set to true and the other one has the same property set to false but there isn't any difference when the button is clicked under the circumstance of false text field validation. The false validation can be provoked by entering negative number or some number with more than 4 decimal digits. The InputVerifier indeed prevents transfering the focus to the other text field but it does not prevent activating the button. Since it does not make sense (at least not to me) the button could be activated without first getting the focus, there shouldn't be a possibility to activate the Start! button when the text field validation method verify() returned false.


EDIT:

I have now changed the code to accommodate the trashgod's suggestion (conditioning the enabled state of the Start! button with FocusListener) and here is the working example:

I have slightly changed the code of verify() method to allow changing the focus if nothing is entered (focusLost() method checks if all text fields contain some input and it also checks if the inputs are valid by explicitly calling verify() for each of the text fields).

The code, of course, needs some minor tweaking (tab order, updating the status, ...) but that is out of the scope of this topic.

Conclusion:

Although VerifyInputWhenFocusTarget property is apparently useful (here in the example the ? button can gain focus even in case of text field(s) validation was false), I am still holding my opinion the documentation is not quite precise in describing all the important side effects which are rather counterintuitive so there is a need of doing further testing and investigations (perhaps analyzing the source code) besides just reading the docs.

0 投票
1 回答
963 浏览

java - JButton 在 setEnabled(true) 之后没有立即获得焦点

我正在尝试制作一个直观的用户界面,用户可以将数值输入到 中JTextFields,用键前进,TAB最后激活按钮以开始处理输入。

一开始按钮是禁用的,只有当所有数据都输入到文本字段时才应该启用。

javax.swing.InputVerifier用来限制只输入最多小数点后 4 位的正数,而且效果很好。

有 3 个可聚焦对象、两个文本字段和按钮。在文本字段中输入(有效)数字后按下该TAB键,并且如果所有文本字段都包含有效输入,则启用该按钮。这也很好。

问题是:
当第一个文本字段已经包含有效数据并按下 时,在第二个文本字段中键入有效数据后TAB按钮没有获得应有的焦点。相反,焦点转移到一行中的下一个可聚焦对象,该对象(再次)是第一个文本字段。

我尝试使用两种不同的方法:

  1. 按钮的enabled属性通过FocusListener内部覆盖focusLost()方法更改
  2. 按钮的enabled属性在覆盖shouldYieldFocus()方法内更改

在这两种情况下,焦点都会在启用按钮后立即跳过按钮。但是,如果我们继续使用TABSHIFT+TAB键更改焦点,按钮将获得应有的焦点 - 就在第二个文本字段之后。

在我看来,opposite组件在启用按钮之前已经预先确定,因此即使在启用按钮之后,按钮也不会获得焦点。

我什至尝试requestFocusInWindow()在启用按钮后强制按钮获得焦点,但这也不起作用,所以问题是如何强制 LayoutFocusTraversalPolicy 重新评估布局,以便它可以立即考虑新引入的按钮禁用前?

这是我尝试过的两种方法的代码:

  1. 按钮的enabled属性通过FocusListenerinsidefocusLost()方法更改:
  1. 按钮的enabled属性在覆盖shouldYieldFocus()方法内更改:

这是测试应用程序的屏幕截图:

测试应用截图

注意:
该代码与我之前提出的问题中包含的代码相关,这是另一个主题。

编辑:
在尝试接受答案的作者提出的建议(invokeLater()用于运行requestFocusInWindow())时,这里是可以作为概念证明的代码:

这只是focusLost()approach #01. 我不知道是否有类似的解决方案可以与approach #02- 一起使用,因为我不知道oppositeshouldYieldFocus()没有FocusListener.

注意:
使用此方案时可以清楚地观察到,输入第二个数字并按下TAB按钮后,焦点首先(暂时)跳转到第一个文本字段,然后才移动到按钮。

0 投票
2 回答
184 浏览

java - getName 方法无法验证来自其他类的 JTextField

我想验证我的其他JTextField使用InputVerifier方法。我做了什么,我为不同的JTextFieldusing设置了一个名称setName

验证

正如您在此处看到的,我正在尝试验证或检查nameString 是否等于,"tfAddress"但不幸的是它不满足条件。任何帮助或提示我该如何解决这个问题?

0 投票
0 回答
56 浏览

java - 在 void 方法中传递类实例

我正在尝试从 Add Employee 类控制按钮,以便在用户输入被接受或不使用InputVerifier时禁用或启用它。我做了什么我为我的addEmployeeButton做了一个 getter 和 setter

我正在从另一个名为 ValidateComponents 的类中访问它。但是该disable方法期望传递一个 AddEmployee 实例。我不能给出一个值,null因为这会给我一个 NullPointerException。

第二次尝试:

当我尝试在方法之外创建 AddEmployee 类的实例时。它给了我一个StackOverFlowError和 this 指向我创建的对象。有人能告诉我解决这个问题的最佳方法是什么吗?任何帮助将不胜感激。

0 投票
1 回答
481 浏览

java - 用户输入验证应该在类级别还是 UI 级别进行?

假设您有一个控制台应用程序:

Main.javaClassA.java

您的主类包含public static void main(String[] args)您运行程序的方法,它使用用户输入作为参数来调用ClassA.

main在将参数发送给方法之前,您是否将用户输入验证放入方法中ClassA

或者

您是否将用户输入验证放在ClassA将消息转回指示输入成功与否的方法中?还是抛出异常?

即在给定无效输入时Integer.Parse(...)抛出。NumberFormatException

0 投票
1 回答
127 浏览

java - 使用取消按钮时如何恢复 InputVerifier 机制?

我在 JFormattedTextField 上有一个 InputVerifier。当场完全失去焦点时调用它。我添加了一个取消按钮(JButton),但不想在单击此按钮时调用 InputVerifier。

MyVerifier 在没有任何问题的情况下在字段上丢失焦点时调用。但是,在我单击 clearButton 后,不会再次调用 InputVerifier,就好像单击了按钮一样,即使它没有被单击。

完整的代码是:

如果在没有单击按钮的情况下字段失去焦点,为什么不调用 InputVerifier?

0 投票
1 回答
49 浏览

python - 为什么当我输入一个空字符串('这里没有')时它会忽略我的 while 条件?

我有这个代码:

当我尝试输入几乎任何内容时,它可以很好地用作验证,但是当它是 '' 时,它只会跳过我的 while 循环。我已经尝试在开始时初始化性别字符串,但它没有帮助:c

0 投票
1 回答
57 浏览

c# - 仅当用户从第二个表单成功登录时,如何使主表单显示?

我正在尝试制作一个验证用户登录的表单。我希望首先显示该表单,如果用户登录成功,则应显示主表单。

这是我尝试过的(第二种形式):

问题是主窗体总是首先弹出。

它应该是这样的:

*然后弹出第二个表单,如果用户被验证,则弹出主表单