更新 JList [英] Updating an JList

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

问题描述

我现在已经创建了一个基于数组列表的 JList,并且由 defaultlistmodel 填充.当他们连接到服务器时,列表会添加人,但不会显示连接的人,或之后连接的人.所以,我必须更新 JList.

I've now made a JList which is based on an arraylist, and is being filled by the defaultlistmodel. The list will add people when they connect to the server, but it will not show the one connecting, or the ones connecting after. So, i have to update the JList.

我的问题是:

我应该更新什么?是否可以使用运行更新的计时器,或者我应该实现一个在有人进入服务器时运行的更新方法?

What should i be updating? Is it possible to use a timer which runs the update, or should i implement an updatemethod which runs when someone enters the server?

ps.这是一个聊天服务器,很像 IRC.

ps. This is an chatserver, much like IRC.

以下是部分代码:

图形用户界面:

jList2 = new javax.swing.JList();
try{
jList2.setModel(gl.getUsersOnlineAsDefaultListModel(gl.getClients())
    );
}catch(RemoteException ex){
    System.out.println(ex);
}
jScrollPane3.setViewportView(jList2);

GUI 逻辑:

public DefaultListModel getUsersOnlineAsDefaultListModel(ArrayList<Client> clients) throws RemoteException {
DefaultListModel result = new DefaultListModel();
for(Client c : clients){
    result.addElement(c.findName());
}
    return result;
}

    public ArrayList<Client> getClients() throws RemoteException, NullPointerException{
            return cf.getClients();
    }

服务器端:

ArrayList clients = new ArrayList<Client>();

public ArrayList<Client> getClients(){
    return clients;          
}

推荐答案

我认为最好的方法是实现由客户端进入更新 JList 的服务器的事件触发的侦听器.

I think the best way to do that is implementing a listener fired by the event the client enters the server which updates the JList.

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

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