在 运行ning GUI 环境中 Java 中 运行 方法的最佳实践

Best practise to run methods in Java on a running GUI enviroment

根据用户输入从 运行 JFrame 启动 运行 方法的最佳/常见做法是什么?

场景 背景是我想连接一个 JFrame 运行 和一个条形码扫描仪。此条形码扫描仪将扫描未预定义(无固定长度)的条形码(带有特殊字符的字母数字),而不是查找像 * 这样的特殊字符并开始操作。

问题 条形码的格式为:* 名字 * 姓氏 * 出生年份。每次 JTextField 的内容发生变化时,我都在考虑 运行 我的 "analysing" 方法,但我认为这是必要的工作。

假设这是扫描的 * 约翰 * 母鹿 * 1988 来自条形码扫描仪的字符进入 "one by one" ,这将导致 13 个不必要的 "content checks" ..这就是我被困的地方。我希望我的解决方案尽可能高效:)

注: 是的,一个解决方案可能是让用户 "confirm his input" 在他扫描后,通过一个按钮或一些动作而不是 "analyse the input" - 但这是我想避免的并使其完全自动化(和高效)

请问您的方法或提示和技巧如何?

SwingWorker is appropriate for this. Read the data in your implementation of doInBackground() and publish() the result when a complete record is available. Your implementation of process() can then safely update a view component's model on the event dispatch thread. As shown in this related example,可以cancel()按需重启worker。如果在一些凭经验确定的时间内没有输入,例如,您可能还需要重置扫描状态。 300 毫秒