用线程填充JList [英] Populate JList with threads

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

问题描述

我希望JList被多个线程填充。
我尝试过这种方式,但jlist为空。
如果快速更新jlist
会有两个线程,另一个线程以另一个方向加载

  new Thread(new Runnable(){
@Override
public void run(){
for(i = 0; i< cells.size()/ 2; i ++ ){
System.out.println( thread);

try {
HtmlPage p = client.getPage( https://tbilisi.embassytools.com/en /slotsReserve?slot=\"+cells.get(i).getAttribute(\"data-slotid));
pages.add(p);
if(!p.getUrl()。toString( ).contains( slotsReserve))
model.add(i,p.getUrl()。toString());
}
catch(Exception e){
e .printStackTrace();
}

}
}
});
list1.setModel(model)

预先感谢



UPDATE *
因此,我使用SwingWorker修复了该问题

解决方案

Swing是一个单线程框架,也就是说,期望对UI的所有更新和修改都是在以下环境中完成的:同样,在EDT中,您不应执行任何可能阻止或阻止其处理事件队列的操作(例如从Web下载内容)。



这引起了一个难题。无法在EDT外部更新UI,需要使用某种后台进程来执行耗时/阻塞的任务...



只要项的顺序无关紧要,例如,可以使用多个 SwingWorker 代替 Thread s或... p>

  DefaultListModel模型=新的DefaultListModel(); 

/*...*/

LoadWorker worker =新的LoadWorker(模型);
worker.execute();

/*...*/

公共类LoaderWorker扩展了SwingWorker< List< URL>,String> {

私人DefaultListModel模型;

public LoaderWorker(DefaultListModel model){
this.model = model;
}

受保护的无效处理(List< String>页面){
for(字符串页面:页面){
model.add(page);
}
}

受保护列表< URL> doInBackground()引发异常{
List< URL> urls = new ArrayList< URL>(25);
for(i = 0; i< cells.size()/ 2; i ++){
try {
HtmlPage p = client.getPage( https://tbilisi.embassytools.com /en/slotsReserve?slot=\"+cells.get(i).getAttribute(\"data-slotid));;
pages.add(p);
if(!p.getUrl()。toString()。contains( slotsReserve)){
publish(p.getUrl()。toString());
urls.add(p.getUrl());
}
}
catch(Exception e){
e.printStackTrace();
}
}
返回网址;
}
}

这使您可以在backround( doInBackground )和发布此方法的结果,然后进行处理

请参见 Swing中的并发以了解更多详情


I want JList to be populated with multiple threads. I tried this way but jlist is empty. It would be good if jlist was updated on the fly There are two threads, the other one loads in anouther direction

            new Thread(new Runnable() {
            @Override
            public void run() {
                for(i=0; i<cells.size()/2; i++){
                    System.out.println("thread");

                    try{
                        HtmlPage p = client.getPage("https://tbilisi.embassytools.com/en/slotsReserve?slot="+cells.get(i).getAttribute("data-slotid"));
                        pages.add(p);
                        if(!p.getUrl().toString().contains("slotsReserve"))
                            model.add(i,p.getUrl().toString());
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }

                }
            }
        });
list1.setModel(model)

Thanks in advance

UPDATE* So I fixed by using SwingWorker

解决方案

Swing is a single threaded framework, that is, it is expected that all updates and modifications to the UI are done from within the context of the Event Dispatching Thread.

Equally, you should do nothing in the EDT that might block or otherwise prevent it from processing the Event Queue (like downloading content from the web).

This raise a conundrum. Can't update the UI outside the EDT, need to use some kind of background process to execute time consuming/blocking tasks...

So long as the order of items is unimportant, you would use multiple SwingWorkers in place o of the Threads, for example...

DefaultListModel model = new DefaultListModel();

/*...*/

LoadWorker worker = new LoadWorker(model);
worker.execute();    

/*...*/

public class LoaderWorker extends SwingWorker<List<URL>, String> {

    private DefaultListModel model;

    public LoaderWorker(DefaultListModel model) {
        this.model = model;
    }

    protected void process(List<String> pages) {
        for (String page : pages) {
            model.add(page);
        }
    }

    protected List<URL> doInBackground() throws Exception {
        List<URL> urls = new ArrayList<URL>(25);
        for(i=0; i<cells.size()/2; i++){
            try{
                HtmlPage p = client.getPage("https://tbilisi.embassytools.com/en/slotsReserve?slot="+cells.get(i).getAttribute("data-slotid"));
                pages.add(p);
                if(!p.getUrl().toString().contains("slotsReserve")) {
                    publish(p.getUrl().toString());
                    urls.add(p.getUrl());
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }        
        }
        return urls;
    }
} 

This allows you execute your blocking/long running in the backround (doInBackground) and publish the results of this method which are then processed within the context of the EDT...

See Concurrency in Swing for more details

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

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