开发者

Use invokeLater to show dialogs one by one

开发者 https://www.devze.com 2023-01-27 18:45 出处:网络
In 2 different action listeners, a dialog will be shown when some conditions are met. If both of the action listeners need to show dialog, 2 dialo开发者_JAVA技巧gs will be shown at the same time. But

In 2 different action listeners, a dialog will be shown when some conditions are met. If both of the action listeners need to show dialog, 2 dialo开发者_JAVA技巧gs will be shown at the same time. But I want them to be shown one by one.

Simplified code:

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(getTopLevelAncestor(), "dialog 1");
            }
        });

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(getTopLevelAncestor(), "dialog 2");
            }
        });

Those 2 "SwingUtilities.invokeLater" invocations are in different classes.


Make a class that keeps track about that; this class would contain a queue of dialogs to display; whenever a dialog is closed, the first one of the queue is shown and removed from the queue. When another class needs to show a dialog, it's immediately shown if the queue is empty, or inserted into the queue else.


This is related to the modality of dialogs. There is quite useful article about this topic http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/modality/. Dialogs have different modality types with different priorities. You can solve your problem by creating the second dialog with lower priority:

JOptionPane pane = new JOptionPane("dialog 2", JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog("Message");
dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
dialog.setVisible(true);

Hopefully this helps.


You need invokeAndWait(), this method waits until the Runnable has been finished.

Or in your situation, when the first dialog has been closed.

0

精彩评论

暂无评论...
验证码 换一张
取 消