2013-01-09 38 views
1

我知道沒有辦法制作JProgressMonitor模式,而寧願使用JDialogJProgressBar。現在,我得到了這個工作 - 但只要我不想製作JDialog模式。誰能告訴我我做錯了什麼?JProgressBar不會更新時,JDialog的模態

private Frame frame; 
private JPanel contentPane; 
private JProgressBar progressBar; 

public MainClass() { 
    JButton startBtn = new JButton("Start"); 
    startBtn.addActionListener(new ActionListener() 
    { 
     @Override 
     public void actionPerformed(final ActionEvent arg0) 
     { 
      new Thread(new Runnable() 
      { 
       @Override 
       public void run() 
       { 
        createJDialog(); 

        for (int i = 0; i < 100; ++i) 
        { 
         final int j = i; 
         doInBackground(); // Batch process 

         SwingUtilities.invokeLater(new Runnable() 
         { 
          @Override 
          public void run() 
          { 
           progressBar.setValue(j); 
          } 
         }); 
        } 
       } 
      }).start(); 
     } 
    }); 
} 

public void createJDialog() 
{ 
    JDialog d = new JDialog(); 
    d.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
    // Keeps progressBar from updating 
    // d.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); 
    // d.setModal(true); 
    d.getContentPane().add(progressBar, BorderLayout.PAGE_START); 
    d.getContentPane().add(progressBar, BorderLayout.PAGE_END); 
    d.pack(); 
    d.setVisible(true); 
} 

回答

3

調用createJDialog();線程啓動後不從內部Runnable

+0

斯坦尼斯拉夫,你讓我的一天!非常感謝! – Bob

2

d.setVisible(true)的調用會阻止,直到對話框爲模態時關閉對話框,按Java API docs

嘗試在單獨的線程中啓動該呼叫。