如何阻止用户生成更多事件?

发布于 2024-12-16 13:10:31 字数 142 浏览 4 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

慕巷 2024-12-23 13:10:32

如果您想禁用所有控件,那么我建议使用GlassPane。请参阅此处了解更多信息: http://docs.oracle.com/javase /tutorial/uiswing/components/rootpane.html

If 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

比忠 2024-12-23 13:10:32

最简单的方法是在单击按钮后立即禁用该按钮,并在执行的操作完成后重新启用它。

The easiest way is to disable the button just after clicking it , and when the action performed is complete then re-enable it.

悲喜皆因你 2024-12-23 13:10:32

您可以禁用按钮,例如 yourButton.setEnabled(false);在您的处理过程中,并在完成后再次启用它。

You can disable your button like yourButton.setEnabled(false); during your processing method and enable it again when it finishes.

浅听莫相离 2024-12-23 13:10:32

看来您想要的是类似 等待光标 的东西。

It seems that what you want is something like a waiting cursor.

意中人 2024-12-23 13:10:31

启用管理是 UI 逻辑的一个组成部分。操作可以帮助您这样做:

 Action action = new AbstractAction("myAction") {

      public void actionPerformed(ActionEvent e) {
           setEnabled(false);
           doPerform();
           setEnabled(true);
      }
 };
 button.setAction(action);

注意:长时间运行的任务不得在 EDT 上执行,因此这仅适用于短期,以防止第二次左右的单击产生效果

编辑< /strong>

只是注意到您用 jsr296 标记了问题,那么它就更容易了:您可以将演示模型的方法标记为 @Action 并将其启用的属性绑定到模型的属性

@Action (enabledProperty == "idle")
public void processOk() {
    setIdle(false);
    doStuff;
    setIdle(true);
}

另外还有支持(有很多争论,但是可用)用于任务:基本上是 SwingWorker,具有细粒度的豆化生命周期支持

Enablement management is an integral part of UI logic. Action helps you doing so:

 Action action = new AbstractAction("myAction") {

      public void actionPerformed(ActionEvent e) {
           setEnabled(false);
           doPerform();
           setEnabled(true);
      }
 };
 button.setAction(action);

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

@Action (enabledProperty == "idle")
public void processOk() {
    setIdle(false);
    doStuff;
    setIdle(true);
}

Plus there is support (much debated, but useable) for Tasks: basically SwingWorker with fine-grained beanified life-cycle support

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文