从问题开始说.
我们产品的 操作台(workbench) 是一个java swing程序. 有一个event handler的代码如下:
public voidactionPerformed(ActionEvent e) {//做耗时的工作, 如插入数据库
}
现在的一个问题在于, 如果用户连续点了两次button, 则event handler被执行两次, 则用户会得到 主键重复错误.
简单的想法是 在event handler的开头结尾把 对应的ok button给disable/enable.
public voidactionPerformed(ActionEvent e) {try{//disable OK 按钮...//做耗时的工作
} finally{//enable OK 按钮...
}
}
但上面这段代码是错误的.
event dispatch thread的工作流程:
enter handling code of event1 -> disable button -> enable button -> enter handling code of event2
正确的实现:
public voidactionPerformed(ActionEvent e) {try{//disable OK 按钮...//做耗时的工作
} finally{
SwingUtilities.invokeLater(newRunnable(){
@Overridepublic voidrun() {//enable OK 按钮...
}});
}
}
event dispatch thread的工作流程:
enter handling code of event1 -> disable button -> ignore event2 -> enable button
If invokeLater is called from the event dispatching thread -- for example, from a JButton's ActionListener -- the doRun.run() will still be deferred until all pending events have been processed.
references:
https://docs.oracle.com/javase/tutorial/uiswing/concurrency/
https://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)
本文探讨了在Java Swing程序中如何防止按钮被快速重复点击导致的问题,特别是当按钮触发的事件处理耗时较长时。文章详细介绍了使用SwingUtilities.invokeLater来确保在当前事件处理完成前忽略后续点击的方法。
1606

被折叠的 条评论
为什么被折叠?



