简体   繁体   English

Java 8 + Swing:模态对话框理论

[英]Java 8 + Swing: Modal Dialog Theory

I am working on an application that will have the following feature: 我正在开发一个具有以下功能的应用程序:

  • The application will have a "Load Image" button to open an image and settings modal dialog. 该应用程序将具有“加载图像”按钮,以打开图像和设置模式对话框。 It will need to block until that dialog returns, either with the results of the processing or null if the user changed his mind. 它需要阻塞直到该对话框返回,否则返回处理结果,或者如果用户改变主意,则返回null。
  • The image and settings dialog will allow the user to select an image using a JFileChooser dialog and to specify to what level of detail to process the image. 图像和设置对话框将允许用户使用JFileChooser对话框选择图像,并指定处理图像的详细程度。 Clicking a "Load" button will open a load dialog. 单击“加载”按钮将打开一个加载对话框。
  • The load dialog needs to be a custom-designed dialog that reports in detail about the time-consuming processing of the image. 加载对话框需要是一个自定义设计的对话框,该对话框详细报告有关费时的图像处理。 If the user allows the processing to finish, it needs to close and return the object back to the original dialog, which needs to close and return that object back to the application. 如果用户允许处理完成,则需要关闭该对象并将其返回到原始对话框,后者需要关闭并将该对象返回给应用程序。 If the user decides it is taking too long to perform the processing, he can cancel the load, closing the loading dialog and returning to the image and settings dialog. 如果用户认为执行处理花费的时间太长,则可以取消加载,关闭加载对话框并返回到图像和设置对话框。

Conceptually, this does not seem so difficult to me. 从概念上讲,这对我来说似乎并不困难。 However, when I try to determine how to get this to work within Swing, somehow I cannot put it together. 但是,当我尝试确定如何使其在Swing中运行时,无法以某种方式将其组合在一起。 From what I've read, GUI components need to be instantiated in Swing's event thread since many of them are not thread-safe. 根据我的阅读,GUI组件需要在Swing的事件线程中实例化,因为其中许多都不是线程安全的。 These same components need to block on calls similar to (but not the same as, since I need to write custom components) the JOptionPane.showInputDialog() methods. 这些相同的组件需要阻塞类似于JOptionPane.showInputDialog()方法的调用(但由于我需要编写自定义组件,所以不相同JOptionPane.showInputDialog() But these calls need to instantiate new components in the event thread and wait for events to occur in the event thread before returning a value to the application. 但是这些调用需要实例化事件线程中的新组件,并等待事件线程中发生事件,然后再将值返回给应用程序。 Compounding this with the fact that I need a dialog to pop up from a dialog, I feel quite lost. 再加上我需要从对话框中弹出一个对话框的事实,我感到很失落。

I have read the Java Tutorial on dialogs and several posts on StackOverflow and other sites trying to determine how I can design classes that work correctly. 我已经阅读了对话框上Java教程,以及StackOverflow和其他站点上的几篇文章,试图确定如何设计可以正常工作的类。 Somehow, I just don't understand how this can work at all (isn't the event thread going to sleep after the first blocking call?), and how I can write the custom classes I need to make this work. 不知何故,我只是根本不了解它是如何工作的(事件线程在第一次阻塞调用后就不会进入睡眠状态?),以及如何编写实现此工作所需的自定义类。 Frankly, I am not certain I understand my confusion enough that I was able to explain it. 坦白说,我不确定自己对混乱的理解程度是否足以解释这一点。

Could someone please explain what goes on under the hood when modal dialogs have been instantiated? 有人可以说明模态对话框实例化后幕后发生的事情吗? How I can write dialog classes that behave the way I need as described above? 我如何编写行为如上所述的对话框类?

The application will have a "Load Image" button to open an image and settings modal dialog. 该应用程序将具有“加载图像”按钮,以打开图像和设置模式对话框。 It will need to block until that dialog returns, either with the results of the processing or null if the user changed his mind. 它需要阻塞直到该对话框返回,否则返回处理结果,或者如果用户改变主意,则返回null。

OK, so this dialog will need to be modal. 确定,因此此对话框将需要是模式对话框。 That much we know. 我们知道的那么多。

The image and settings dialog will allow the user to select an image using a JFileChooser dialog and to specify to what level of detail to process the image. 图像和设置对话框将允许用户使用JFileChooser对话框选择图像,并指定处理图像的详细程度。 Clicking a "Load" button will open a load dialog. 单击“加载”按钮将打开一个加载对话框。

OK, so the load dialog will need to be modal off of the image and settings dialog. 单击确定,因此需要在图像和设置对话框的前面关闭加载对话框。 No biggie there either. 那里也没有大佬。

The load dialog needs to be a custom-designed dialog that reports in detail about the time-consuming processing of the image. 加载对话框需要是一个自定义设计的对话框,该对话框详细报告有关费时的图像处理。 If the user allows the processing to finish, it needs to close and return the object back to the original dialog, which needs to close and return that object back to the application. 如果用户允许处理完成,则需要关闭该对象并将其返回到原始对话框,后者需要关闭并将该对象返回给应用程序。 If the user decides it is taking too long to perform the processing, he can cancel the load, closing the loading dialog and returning to the image and settings dialog. 如果用户认为执行处理花费的时间太长,则可以取消加载,关闭加载对话框并返回到图像和设置对话框。

OK, so the load dialog code will need to instantiate and execute a SwingWorker to do the time-consuming image processing in a background thread, and then have the SwingWorker use its publish/process method pair to push information about the processing details back to the load dialog. 好的,因此加载对话框代码将需要实例化并执行SwingWorker以在后台线程中进行耗时的图像处理,然后让SwingWorker使用其发布/处理方法对将有关处理详细信息的信息推回给加载对话框。

...From what I've read, GUI components need to be instantiated in Swing's event thread since many of them are not thread-safe. ...据我所读,GUI组件需要在Swing的事件线程中实例化,因为许多组件都不是线程安全的。

Correct. 正确。

These same components need to block on calls similar to (but not the same as, since I need to write custom components) the JOptionPane.showInputDialog() methods. 这些相同的组件需要阻止类似于JOptionPane.showInputDialog()方法的调用(但由于我需要编写自定义组件,所以不相同)。

And this is what a modal JDialog allows you to do. 这就是模态JDialog允许您执行的操作。 Another option to keep in mind is to use a JOptionPane and pass in a JPanel with whatever GUI you want the JOptionPane to display. 要记住的另一种选择是使用JOptionPane并将JPanel传递给想要JOptionPane显示的任何GUI。 JOptionPanes are surprisingly flexible and useful. JOptionPanes出奇地灵活和有用。

But these calls need to instantiate new components in the event thread and wait for events to occur in the event thread before returning a value to the application. 但是这些调用需要实例化事件线程中的新组件,并等待事件线程中发生事件,然后再将值返回给应用程序。 Compounding this with the fact that I need a dialog to pop up from a dialog, I feel quite lost. 再加上我需要从对话框中弹出一个对话框的事实,我感到很失落。

Again it's simple. 再次简单。 The load dialog will call a SwingWorker which will communicate back to the load dialog. 加载对话框将调用SwingWorker,该SwingWorker将与加载对话框进行通信。

Could someone please explain what goes on under the hood when modal dialogs have been instantiated? 有人可以说明模态对话框实例化后幕后发生的事情吗?

Now you may be asking a bit too much for the volunteers on this site to do, since this question would probably require someone to write a complete tutorial to answer, and it has been asked and answered before, so the information should be discoverable by you. 现在您可能要求这个站点上的志愿者做太多事情,因为这个问题可能需要有人编写完整的教程来回答,并且之前已经有人问过并回答过,因此您应该可以发现这些信息。 。 If you really want to see what is going on under the hood, you should first do the preliminary research on the subject yourself, look at the source code, and if still stuck, ask a much more specific and answerable question after first doing your own due diligence work. 如果您真的想了解实际情况,则应首先自己对该主题进行初步研究,查看源代码,如果仍然遇到问题,请先做一个更具体,可回答的问题尽职调查工作。

Modal dialogs started from the primary event loop spawn a secondary event loop that remains active while the primary loop is blocked. 从主要事件循环开始的模态对话框会生成一个次要事件循环,当主要循环被阻止时,该活动仍保持活动状态。 See java.awt.SecondaryLoop. 参见java.awt.SecondaryLoop。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM