Swing线程中的Java操作 [英] Java operations in Swing thread

查看:82
本文介绍了Swing线程中的Java操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题. 这是代码:

I have a problem. Here is the code:

JButton buttonChangeServer = new JButton("Change server");
    buttonChangeServer.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
                getLobbies();
            }
        }
    });

private void getLobbies() {
    lobbyListModel.removeAllElements();
    statusLabel.setText("Connecting...");
    final ArrayList<LobbyInfo> lobbyInfos = 
            UserClient.getLobbies(host, action, null);
    if (lobbyInfos != null) {
        setLobbies(lobbyInfos);
        statusLabel.setText("Sucessfully got lobby list from " + getHost());
    }
    else {
        statusLabel.setText("Failed to connect to " + getHost());
    }
}

如果UserClient.getLobbies(host, action, null)方法无法建立连接,则会执行3秒钟(超时). 问题是这两个操作

The UserClient.getLobbies(host, action, null) method executes for a 3 seconds (timeout) if it can not establish connection. The problem is that this two operations

lobbyListModel.removeAllElements();
statusLabel.setText("Connecting...");

在执行时不可见.

我认为问题在于actionPerformed(ActionEvent e)中的getLobbies()方法在Swing线程中执行,并且直到getLobbies();

I suppose that the problem is that the method getLobbies() in actionPerformed(ActionEvent e) executes in Swing thread, and all the GUI operations are not being shown till the end of the execution of the getLobbies();

我的目的是显示在执行UserClient.getLobbies(host, action, null);之前和之后GUI的所有更改.我该如何处理?有没有简单的方法可以显示所有这些?谢谢.

My aim is to show all the changes of GUI, before and after the execution of UserClient.getLobbies(host, action, null);. How can I manage that? Is there an easy way to show all of them? Thank you.

P.S..解决方案之一可能是将可能需要很长时间的操作放在另一个线程中,例如:

P.S. One of the solutions may be putting that potentionally long operation in another thread, like this:

private void getLobbies() {
        lobbyListModel.removeAllElements();
        statusLabel.setText("Connecting...");
        new Thread(new Runnable() {
            @Override
            public void run() {
                final ArrayList<LobbyInfo> lobbyInfos = 
                    UserClient.getLobbies(host, action, null);
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        if (lobbyInfos != null) {
                            setLobbies(lobbyInfos);
                            statusLabel.setText("Sucessfully got lobby list from " + getHost());
                        }
                        else {
                            statusLabel.setText("Failed to connect to " + getHost());
                        }
                    }
                });
            }
        }).start();
    }

它有效,但是相当复杂.有没有更简单的方法?

It works, but it is rather complicated. Are there any ways easier?

推荐答案

其中一种解决方案可能是将可能需要很长时间的操作放在另一个线程中

One of the solutions may be putting that potentially long operation in another thread

是的,不应在EDT上执行长操作(或阻塞操作).

Yes, long operations (or blocking operations) should not execute on the EDT.

因此,您确实需要在单独的线程上执行长时间运行的任务.在 Worker线程和Swing Worker 针对该问题的Swing解决方案.

So you do need to execute the long running task on a separated Thread. Check out the section from the Swing tutorial on Worker Threads and Swing Worker for the Swing solution to this problem.

查询执行完毕后,您可以发布"结果,以便在更新Swing组件时在EDT上执行代码.

When your query finishes executing you can "publish" the results so the code is executed on the EDT when the Swing components are updated.

这篇关于Swing线程中的Java操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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