java - 使用 FocusListener 从 JTextField 检索内容并填充其他 JTextField
问题描述
我的窗口中有许多组件,其中大部分是 JTextField。为了节省用户输入数据的时间,我尝试在填充时解析第一个 JTextField 的内容,并将建议的值放入后续的 JTextField 中。
我已将 FocusListener 添加到第一个 JTextField,并在 focusLost() 中检索其内容,然后设置后续 JTextField 的内容。这在第一次使用应用程序时可以正常工作。然而,在后续任务中(即在处理完所有数据并清除字段之后)它通常(但不总是)失败。焦点问题的调试很困难,所以我改用日志记录,可以看到在失败的情况下,第一个 JTextField 上的 getText() 方法返回 null。这一定是时间问题,但我看不到解决方法。
focusLost() 方法开始...
@Override
public void focusLost(FocusEvent e) {
// On losing focus of the title field attempt to populate all the other
// resource detail fields
Component rawComponent = e.getComponent();
if (this.resourceTitleTextField == rawComponent) {
JTextField component = (JTextField) rawComponent;
String title = component.getText();
LOGGER.info("title = " + title);
解决方案
推荐阅读
- google-analytics - 是否可以使用 gtag.js 通过自定义参数对 Google Analytics 中的用户进行分组?
- linux - 如何将彩色文本输出到标准输出,同时将未着色的文本附加到文件中?
- angular - 在 Angular 打字稿中重构重复的代码
- css - 最大化离子网格中的列数(24 列)(自定义离子网格列)
- coldfusion - 3 使用 Coldfusion 进行相关选择
- sql-server - 获取 COMPATIBILITY_LEVEL 的历史记录
- python - 删除重复的模式(荷兰号码和邮政编码)正则表达式
- c# - NET Core 3.1 工作人员服务在 IIS 中
- maven - 无法执行目标 org.apache.tomcat.maven:tomcat7-maven-plugin:2.0:deploy 无法调用 Tomcat 管理器:对等方重置连接
- ios - 如何在运行时从 Xcode 记录崩溃/异常