试图用Java创建一个模态“生成报告”对话框

时间:2011-12-07 17:12:31

标签: java multithreading swing jdialog runnable

因此,我有一个报告应用程序,需要一段时间才能生成报告,用户抱怨报告没有运行的视觉反馈。

我写了一个小类,它将成为一个模态对话框,阻止用户做任何事情,用“生成报告......”这个短语展示自己,直到报告实际完成然后它会隐藏本身和经常使用将返回。

我遇到的问题是对话框出现,文本出现,但问题是传递给它的runnable没有运行。

这是忙碌的对话框:

package com.company.utilities.busydialog;

import java.awt.BorderLayout;
import java.awt.Cursor;
import javax.swing.JDialog;
import javax.swing.JLabel;

public class BusyDialog extends JDialog {

    private Runnable r;

    public BusyDialog (String Message, Runnable r) {
    super();
        this.r = r;
    this.setModal(true);

    this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

    this.setLayout(new BorderLayout());

    this.getContentPane().add(new JLabel(Message));

    this.pack();
    }

    public void show() {
    this.setVisible(true);
        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        System.out.println("running report");
    r.run();
        this.setCursor(Cursor.getDefaultCursor());
    this.setVisible(false);
    }
} 

以下是我称之为的方法:

private void dailyUsageSubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                       
    final Date date = this.dailyUsagePicker.getDate();
    final ReportController c = this.controller;
    final ProductionHRClientView view = this;
    BusyDialog dialog = new BusyDialog("Generating report...", new Runnable() {
        public void run() {
            c.generateDailyUsageReport(date, view);
        }
    });
    dialog.setResizable(false);
    dialog.setLocation(700,400);
    dialog.Show();
}    



EDIT: I tried this http://stackoverflow.com/questions/4542580/how-to-make-a-modal-jdialog-execute-code-immediately-upon-being-shown

结束了BusyDialog的这个课程:

package com.protocase.utilities.busydialog;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import javax.swing.JLabel;

public class BusyDialog extends JDialog {

    public BusyDialog(String Message, final Runnable r) {
        super();
        this.setModal(true);
        this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.getContentPane().add(new JLabel(Message));
        this.pack();
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowOpened(WindowEvent e) {
                super.windowOpened(e);
                // do something
                doBusy(r);

            }
        });
    }

    private final void doBusy(Runnable r) {
        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        r.run();
        this.setCursor(Cursor.getDefaultCursor());
        this.dispose();
    }


}

但这也无法解决问题。

2 个答案:

答案 0 :(得分:2)

Swing中的所有JComponents必须在EDT上完成,更多在Concurency in Swing

中完成

然后你必须:

1)this.setVisible(true);应该移动到构造函数

中的最后一个代码行

2)this.setVisible(true);将包含在invokeLater()

3)是否有长时间运行的任务,那么你必须寻找invokeAndWait()

答案 1 :(得分:1)

试试这个

package com.protocase.utils.dialogs;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import javax.swing.JLabel;

public class BusyDlg extends JDialog {

    private Runnable r;

    public BusyDlg (String Message, Runnable r) {
    super();
        this.r = r;
    this.setModal(true);

        this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

        this.setLayout(new BorderLayout());

    this.getContentPane().add(new JLabel(Message));

        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowOpened(WindowEvent e) {
                super.windowOpened(e);
                // do something
                doBusy();
            }
        });




    this.pack();
    }

    public void Show() {
    this.setVisible(true);
    }

    public void doBusy() {
        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        r.run();
        this.setCursor(Cursor.getDefaultCursor());
    this.setVisible(false);
        this.dispose();
    }
}