使用的SwingWorker以在GUI中添加一个进度条(Using SwingWorker to a

2019-07-20 01:50发布

我用的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);

            }

        }
     }
 }

}

和第二类(我写一张本)

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

    }

当我在测试推出第二类,显示消息“未找到方法”之前出现进度条,而我想后出现的情况下,消息。 该怎么办?

Answer 1:

task.execute()将推出背景(其中doInBackground方法将从被调用),程序将继续执行。

task.execute()不是阻塞方法,这是使用它的原因,这样你就不会阻塞事件调度线程

您可以监视状态SwingWorkerPropertyChangeListener

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


Answer 2:

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

你不必担心线程问题,因为这种方法被称为在美国东部时间了。



文章来源: Using SwingWorker to add a progress bar in a GUI