java - 使用 SwingWorker 在 GUI 中添加进度条

标签 java multithreading swing user-interface progress-bar

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

    }

当我在第二类测试器中启动时,在出现进度条之前会显示消息“找不到方法”,而我希望出现消息以防万一 后。该怎么办?

最佳答案

task.execute() 将启动一个后台(其中将调用 doInBackground 方法),并且程序将继续执行。

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

您可以使用 PropertyChangeListener 监视 SwingWorker 的状态

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

关于java - 使用 SwingWorker 在 GUI 中添加进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14928213/

相关文章:

java - 在 Eclipse 中运行我签名的发布 keystore 时出现问题

c++ - 等待线程结束 (C++)

ios - 崩溃 : com. apple.root.default-qos

c - 得到错误的答案使用线程安全计数器

java - 使用 Ant 从同一代码库构建 Swing 和 Android 应用程序

java - 使用隐式 lambda 时出现不兼容类型错误

java - 将节点转换为对象

java - 在 View 寻呼机中加载网页 View 的时间太多

java - 如何与另一件 JComboBox 元素交换?

java - JTextField 和 keyListener java swing