java 在 GWT 中创建模态对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7129934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:45:26  来源:igfitidea点击:

Creating a modal dialog in GWT

javagwtcallbacksynchronization

提问by Mitch

I'm a GWT and Java newbie trying to figure out how to create a synchronous (modal) dialog in GWT, but I'm having difficulty. The DialogBox class has a flag that says:

我是 GWT 和 Java 新手,试图弄清楚如何在 GWT 中创建同步(模态)对话框,但我遇到了困难。DialogBox 类有一个标志,上面写着:

modal - true if keyboard and mouse events for widgets not contained by the dialog should be ignored

modal - 如果对话框中不包含的小部件的键盘和鼠标事件应该被忽略,则为真

But that doesn't make the dialog synchronous.

但这不会使对话框同步。

I read one suggestion that said that I need to place the code that processes the dialog's data entered by the user inside the OK button handler. I don't like that because it makes the dialog responsible for displaying the data and processing it too. This results in a poor "Separation of Concerns" and violates the "Single Responsibility Principle" of good design.

我读到一个建议,它说我需要将处理用户输入的对话框数据的代码放在 OK 按钮处理程序中。我不喜欢那样,因为它使对话框负责显示数据和处理数据。这导致糟糕的“关注点分离”并违反了良好设计的“单一职责原则”。

I placed the code I want to execute in the OK button handler and now want to have my GWT dialog used in two places in my code. In one case, the user's data from the dialog is added to a table when the OK button is pressed. In the other case, the data in the table is modified when the OK button is pressed. If I could create a truly synchronous dialog, I could handle the OK button outside the dialog when it returns from execution and easily reused the dialog.

我将要执行的代码放在 OK 按钮处理程序中,现在希望在我的代码中的两个地方使用我的 GWT 对话框。在一种情况下,当按下“确定”按钮时,对话框中的用户数据将添加到表格中。在另一种情况下,表中的数据在按下 OK 按钮时被修改。如果我可以创建一个真正同步的对话框,我就可以在它从执行返回时处理对话框外的 OK 按钮,并且可以轻松地重用该对话框。

If forced to use my current asynchronous model, I'll need to pass in additional data as described here. This seems like a lot of work for such a common task. Did I miss something? Are there other options?

如果被迫使用我当前的异步模型,我将需要按照此处所述传入其他数据。对于这样一个常见的任务,这似乎需要做很多工作。我错过了什么?还有其他选择吗?

采纳答案by filip-fku

The modal flag basically makes the dialog take the focus of the UI. So nothing else can be clicked or interacted with except for what is in the dialog.

模态标志基本上使对话框成为 UI 的焦点。因此,除了对话框中的内容之外,无法单击或交互任何其他内容。

Unfortunately as you'll know, JavaScript (GWT) is inherently asynchronous - in the browser there is no JS code executing until the user does something to trigger an event. So it is events we have to respond to.

不幸的是,正如您所知,JavaScript (GWT) 本质上是异步的 - 在浏览器中,在用户执行某些操作来触发事件之前,不会执行 JS 代码。所以这是我们必须回应的事件。

As far as I am aware GWT does not provide a synchronous dialog. If it did, it would mean that a blocking section of code is introduced. With JavaScript being single threaded, that would lock up your application in busy waiting - meaning that you couldn't do any meaningful processing in the dialog itself or anywhere else for that matter.

据我所知,GWT 不提供同步对话框。如果是这样,则意味着引入了代码的阻塞部分。由于 JavaScript 是单线程的,这会将您的应用程序锁定在忙等待状态 - 这意味着您无法在对话框本身或其他任何地方进行任何有意义的处理。

This is how I would handle it:

这就是我将如何处理它:

  • Create my own modal dialog class, wrapping a GWT DialogBox where I would implement the dialog outcomes eg: SAVE / OK / CANCEL buttons.
  • Provide a notification event or callback - onDialogExit for example, where the dialog result / OK / CANCEL / SAVE / ANYTHING would be provided to an interested party. When the user takes an action (such as clicking a button) to exit the dialog, simply raise the correct event. The event could also return a value if some data needs to result from the dialog.
  • Provide a callback for the event where ever dialog outcomes need to be handled.
  • 创建我自己的模态对话框类,包装一个 GWT DialogBox,我将在其中实现对话框结果,例如:SAVE / OK / CANCEL 按钮。
  • 提供通知事件或回调 - 例如 onDialogExit,其中对话结果 / OK / CANCEL / SAVE / ANYTHING 将提供给感兴趣的一方。当用户采取操作(例如单击按钮)退出对话框时,只需引发正确的事件。如果某些数据需要从对话框中产生,该事件也可以返回一个值。
  • 为需要处理对话结果的事件提供回调。

This is very similar to the function pointer link you had mentioned - you'd just be writing your own event.

这与您提到的函数指针链接非常相​​似 - 您只需编写自己的事件。

回答by B. Miller

There is a better way than the asynchronous approach.

有比异步方法更好的方法。

boolean okPressed = Window.confirm("Your Message Here");

Your application holds until the user presses 'ok' or 'cancel' and you can process the boolean returned.

您的应用程序会一直保持,直到用户按下“确定”或“取消”,您才能处理返回的布尔值。

Unfortunately, you cannot change the text of the 'ok' and 'cancel' buttons.

不幸的是,您无法更改“确定”和“取消”按钮的文本。

回答by maneesh

Check out DialogBox. You can use one of the constructors to make it modal.

查看对话框。您可以使用其中一个构造函数使其成为模态。