0

SwingWorker を使用して、Java Swing API でプログレス バーを作成します。

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);

            }

        }
     }
 }

}

そして2番目のクラス(私はこれの一部を書いています)

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

    }

テスターで2番目のクラスを起動すると、プログレスバーが表示される前に「メソッドが見つかりません」というメッセージが表示されますが、後で表示されるメッセージが表示されます。何をすべきか?

4

2 に答える 2

3

task.execute()バックグラウンド(doInBackgroundメソッドが呼び出される場所)を起動し、プログラムは実行を継続します。

task.execute()はブロック方法ではありません。これが使用理由であるため、イベントディスパッチスレッドをブロックしません。

SwingWorkerの状態を監視することができます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();
于 2013-02-18T01:49:26.200 に答える
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 でも呼び出されるため、スレッドの問題について心配する必要はありません。

于 2013-02-18T01:46:32.160 に答える