如何阻止用户生成更多事件?
我正在开发一个 java swing 桌面应用程序。对话框有一个确定和取消按钮。当用户单击“确定”按钮时,应用程序会执行一些处理。在“确定”按钮上的事件完成执行之前,如何阻止用户再次单击“确定”。另外,我不希望用户能够按下取消按钮,直到确定完成执行。任何帮助将不胜感激。
I am developing a java swing desktop application. The dialog form has an ok and cancel button. When the user clicks ok button the application does some processing. How can I stop user from clicking ok again before the event on ok button has finished executing. Also, i dont want the user to able to press the cancel button till ok has finished executed. Any help will be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想禁用所有控件,那么我建议使用
GlassPane
。请参阅此处了解更多信息: http://docs.oracle.com/javase /tutorial/uiswing/components/rootpane.htmlIf you want to disable all the controls, then I'd suggest using a
GlassPane
. See here for more info: http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html最简单的方法是在单击按钮后立即禁用该按钮,并在执行的操作完成后重新启用它。
The easiest way is to disable the button just after clicking it , and when the action performed is complete then re-enable it.
您可以禁用按钮,例如 yourButton.setEnabled(false);在您的处理过程中,并在完成后再次启用它。
You can disable your button like yourButton.setEnabled(false); during your processing method and enable it again when it finishes.
看来您想要的是类似 等待光标 的东西。
It seems that what you want is something like a waiting cursor.
启用管理是 UI 逻辑的一个组成部分。操作可以帮助您这样做:
注意:长时间运行的任务不得在 EDT 上执行,因此这仅适用于短期,以防止第二次左右的单击产生效果
编辑< /strong>
只是注意到您用 jsr296 标记了问题,那么它就更容易了:您可以将演示模型的方法标记为 @Action 并将其启用的属性绑定到模型的属性
另外还有支持(有很多争论,但是可用)用于任务:基本上是 SwingWorker,具有细粒度的豆化生命周期支持
Enablement management is an integral part of UI logic. Action helps you doing so:
Beware: long running task must not be executed on the EDT, so this is for short-term only, to prevent the second or so click having an effect
Edit
just noticed that you tagged the question with jsr296, then it's even easier: you can tag a method of your presentation model as @Action and bind its enabled property to a property of the model
Plus there is support (much debated, but useable) for Tasks: basically SwingWorker with fine-grained beanified life-cycle support