简体   繁体   English

带有JDialog的Applet无法在Mac OSX中正确隐藏

[英]Applet with JDialog not hiding correctly in Mac OSX

I have an applet that calls a JDialog that contains a JProgressBar component. 我有一个小程序,它调用包含JProgressBar组件的JDialog。 I subclass the JDialog to expose a method to update the JProgressBar, something like: 我将JDialog子类化,以公开更新JProgressBar的方法,例如:

public class ProgressDialog extends javax.swing.JDialog {
    public void setProgress(double progress) {
        jProgressBar1.setValue(jProgressBar1.getMinimum() + (int) (progress * jProgressBar1.getMaximum()));
    }
    ...
}

I use this dialog in the following manner: 我以以下方式使用此对话框:

public void test() throws Exception {
    progressDialog = new ProgressDialog(null, true);

    try {
        progressDialog.setLocationRelativeTo(null);

        // show the dialog
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                progressDialog.setVisible(true);
            }
        });

        // business logic code that calls progressDialog.setProgress along the way
        doStuff();

    } finally {
        progressDialog.setVisible(false);
        progressDialog.dispose();
    }
}

It works fine on Windows/any browser. 在Windows /任何浏览器上都可以正常工作。 However, when invoking the above function on Firefox 2/3/3.5 on a Mac, the progressDialog is displayed indefinitely, ie it doesn't close. 但是,在Mac上的Firefox 2/3 / 3.5上调用上述功能时,progressDialog将无限期显示,即不会关闭。

I suspected that calling setVisible(true) inside the EventQueue was causing the problem, since it's a blocking call and might block the queue completely, so I tried changing it to: 我怀疑在EventQueue中调用setVisible(true)会导致此问题,因为这是一个阻塞调用,并且可能会完全阻塞队列,因此我尝试将其更改为:

        // show the dialog
        new Thread() {
            public void run() {
                progressDialog.setVisible(true);
            }
        }.start();

With this change, the progressDialog now closes correctly, but a new problem emerged - the contents of the dialog (which included the progressbar, an icon and a JLabel used to show a message string) were no longer shown inside the dialog. 进行此更改后,progressDialog现在可以正确关闭,但是出现了一个新问题-对话框的内容(包括进度条,用于显示消息字符串的图标和JLabel)不再显示在对话框中。 It was still a problem only on Mac Firefox. 仅在Mac Firefox上仍然是一个问题。

Any ideas? 有任何想法吗? I realize it's probably some AWT threading issue, but I've been at this for a couple of days and can't find a good solution. 我意识到这可能是一些AWT线程问题,但是我已经待了几天,却找不到一个好的解决方案。 Wrapping the doStuff() business logic in a separate new Thread seems to work, but it's not easy to refactor the actual business logic code into a separate thread, so I'm hoping there's a simpler solution. 将doStuff()业务逻辑包装在单独的新线程中似乎可行,但是将实际业务逻辑代码重构到单独的线程中并不容易,因此我希望有一个更简单的解决方案。

The envt is: Mac OSX 10.5 Java 1.5 Firefox 2/3/3.5 信封是:Mac OSX 10.5 Java 1.5 Firefox 2/3 / 3.5

Found out that the problem was that the applet function was executing inside the AWT dispatcher thread, therefore the thread blocks and no events are processed until the applet function finishes execution. 发现问题是applet函数在AWT调度程序线程中执行,因此线程阻塞,直到applet函数完成执行,才处理任何事件。

Solution was to move the processing logic into a separate thread spawned by the ProgressDialog object before calling setVisible(true). 解决方案是在调用setVisible(true)之前,将处理逻辑移到由ProgressDialog对象生成的单独线程中。 setVisible(true) would block the main thread but still allow the event dispatcher to continue processing, hence rendering the contents of the dialog until the spawned thread calls setVisible(false) to hide the dialog. setVisible(true)将阻止主线程,但仍允许事件分发程序继续处理,因此呈现对话框的内容,直到生成的线程调用setVisible(false)来隐藏对话框。

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

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