使用swingworker线程更新UI [英] Update UI using swingworker thread

查看:457
本文介绍了使用swingworker线程更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用swing工作线程来更新我的GUI。请帮忙。我需要使用线程即setText()更新1字段的状态。

I want to use the swing worker thread to update my GUI in swing. pls any help is appreciated.I need to update only the status of 1 field using the thread i.e setText().

推荐答案

我只是在另一个论坛上回答有关SwingWorker问题的类似问题:

I just answer similar question on another forum for a question about SwingWorker:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.Timer;


public class Main extends JFrame 
{
    private JLabel label;
    private Executor executor = Executors.newCachedThreadPool();
    private Timer timer;
    private int delay = 1000; // every 1 second

    public Main()
    {
        super("Number Generator");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300, 65);
        label = new JLabel("0");
        setLayout(new FlowLayout());
        getContentPane().add(label, "Center");
        prepareStartShedule();
        setVisible(true);
    }
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new Main();
            }
        });
    }

    private void prepareStartShedule()
    {
        timer = new Timer(delay, startCycle());
        timer.start();
    }

    private Action startCycle()
    {
        return new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                executor.execute(new MyTask());
            }
        };
    }

    private class MyTask extends SwingWorker<Void, Integer>
    {
        @Override
        protected Void doInBackground() throws Exception
        {
            doTasksInBackground();
            return null;
        }

        private void doTasksInBackground()
        {
            publish(generateRandomNumber());
        }

        private int generateRandomNumber()
        {
            return (int) (Math.random() * 101);
        }

        @Override
        protected void process(List<Integer> chunks)
        {
            for(Integer chunk : chunks) label.setText("" + chunk);
        }

    }
}

ps :@trashgod帮我一个月前了解如何处理SwingWorker(如果线程启动Executor,则无法从Future<?>和SwingWorker获取ArrayIndexOutOfBoundsException,所以感谢他。

ps: @trashgod helps me a month ago to understand how to deal with SwingWorker (Can't get ArrayIndexOutOfBoundsException from Future<?> and SwingWorker if thread starts Executor), so thanks to him.

编辑:代码已更正。谢谢@ 充满鳗鱼的气垫船

The code is corrected. Thanks @Hovercraft Full Of Eels

这篇关于使用swingworker线程更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆