2011-12-07 13 views
1

因此,我有一個報告應用程序需要一段時間來生成報告,並且用戶抱怨報告沒有運行的視覺反饋。試圖在Java中創建一個模式「生成報告」對話框

我已經寫了一個小型的類,它將成爲阻止用戶做任何事情的模態對話框,在報告實際完成之前用「生成報告....」這個短語展示自己,然後它會隱藏自己並定期使用會返回。

我遇到的問題是出現對話框,文本出現,但問題是傳遞給它的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(); 
    } 


} 

但這也不能解決問題。

+0

爲什麼不使用進度條? – cdeszaq

+0

我仍然需要使它成爲模態並使其運行線程。 – davidahines

回答

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(); 
    } 
} 
2

在Swing所有JComponent上必須在美國東部時間來完成,更Concurency in Swing

,那麼你必須:

1)this.setVisible(true);應該移動到最後一行代碼在構造

2) this.setVisible(true);將裏面invokeLater()

3)是否有被包梁長時間運行的任務(一個或多個),那麼你必須尋找invokeAndWait()

相關問題