2013-02-18 21 views
0

我使用SwingWorker通過Java Swing API創建進度條。使用SwingWorker在GUI中添加進度條

我有一個擴展的SwingWorker

class Swinger extends SwingWorker { 
private ClassAnalyzer classAnalyzer; 

public Swinger(ClassAnalyzer classAnalyzer){ 
    this.classAnalyzer = classAnalyzer; 
} 
     @Override 
     public Void doInBackground() throws InterruptedException { 

      try 
     {  
      int progress = 0; 
      while (progress < 100) { 

// at this point I make certain elaboration on classAnalyzer     

       progress++; 

       //Call the process method to update the GUI 
       publish(progress); 

      }      
     } 
     catch(InterruptedException e) 
     { 
     } 
     return null; 
    } 

    @Override 
    protected void process(List chunks) { 
    for (Integer chunk : chunks) { 
     progressBar.setValue(chunk); 

     //if the switchtype checkbox is selected then 
     //change the progressbar to a determined type 
     //once the progress has reached 50 
     if (chunk > 49) 
     { 
      if (switchType.isEnabled() && switchType.isSelected()) 
      { 
       progressBar.setStringPainted(true); 

      } 

     } 
    } 
} 

} 

和第二類(我寫一張本)

public Tester() 
{ 
    JFrame guiFrame = new JFrame(); 

    //make sure the program exits when the frame closes 
    guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    guiFrame.setTitle("Creating a Table Example"); 
    guiFrame.setSize(700,200); 

    //This will center the JFrame in the middle of the screen 
    guiFrame.setLocationRelativeTo(null); 


    goButton = new JButton("Go"); 
    goButton.setActionCommand("Go"); 
    goButton.addActionListener(new ActionListener() 
    { 

     //When the button is clicked the SwingWorker class is executed and 
     //the button is disabled 
     @Override 
     public void actionPerformed(ActionEvent event) 
     { 

      progressBar.setStringPainted(progressType.isSelected()); 
      ClassAnalyzer c = new ClassAnalyzer(); 
      Swinger task = new Swinger(c); 
      task.execute(); 

      int methods = c.getNumberOfMethods(); 

      if(methods == 0){ 
      JOptionPane.showMessageDialogo(null, "methods not found"); 
      } 

      goButton.setEnabled(false); 
     } 
    }); 

    } 

當我在測試推出第二類,A類在出現進度條之前顯示消息「找不到方法」,而我希望消息在出現 之後出現。該怎麼辦?

+0

監控SwingWorker的狀態你的另一個問題是一個單獨的問題。無論您的第二個問題如何,您都應該接受這裏發佈的答案之一。 – 2013-02-18 17:16:39

回答

3

task.execute()將啓動一個背景(其中將調用doInBackground方法)並且程序將繼續執行。

task.execute()不是阻塞方法,這是使用它的原因,這樣你就不會阻塞事件調度線程

您可以用PropertyChangeListener

final ClassAnalyzer c = new ClassAnalyzer(); 
Swinger task = new Swinger(c); 
task.addPropertyChangeListener(new PropertyChangeListener() { 
    @Override 
    public void propertyChange(PropertyChangeEvent evt) { 
     if (evt.getPropertyName().equals("state") && evt.getNewValue().equals(SwingWorker.StateValue.DONE)) { 
      int methods = c.getNumberOfMethods(); 

      if(methods == 0){ 
       JOptionPane.showMessageDialogo(null, "methods not found"); 
      } 
     } 
    } 
}); 
task.execute(); 
3

SwingWorker類還定義了一個done方法,您可以在其中定義完成任務後要執行的操作。

class Swinger extends SwingWorker { 

    // all the rest of your code ^^ 

    @Override 
    protected void done() { 
     if(this.classAnalyzer.getNumberOfMethods() == 0) 
      JOptionPane.showMessageDialog(null, "methods not found");    
    } 
} 

因爲在EDT上也調用了此方法,所以您不必擔心線程問題。