我创建了一个聊天程序,客户端连接服务器。客户端能够加入通道,当它们这样做时,jList(请参阅代码)应该通过DefaultListModel更新(getChannel()),并且应该显示连接到同一通道的所有其他客户端。我知道getChannel();工作,因为我已经调试它(并使用systemoutprint)。我已经有活动频道和用户在线的Jlists,这有效! :)
客户端使用回调方法通知连接到服务器的其他客户端。
我已经完成了一些调试,并且我一直在尝试重复我的代码。
服务器
private Hashtable<String, ArrayList<String>> channels = new Hashtable<String, ArrayList<String>>();
public void connectChannel(String username, String channel) throws RemoteException{
setUsername(username);
if(isUserRegistered(username)){
if (!channels.containsKey(channel)) {
String message = "User " + username + " entered the channel";
channels.put(channel, new ArrayList<String>());
channels.get(channel).add(username);
notifyChannelSystem(channel, "SYSTEM", message);
notifySelf(username, "Write /? for avaliable commands");
System.out.println("User " + username + " connected to channel " + channel + " which was created");
}
else{
if(channels.get(channel).contains(username)){
notifySelf(username, "Place in channel " + channel);
System.out.println("User " + username + " connected to channel " + channel + " which the user was already in");
}
else {
channels.get(channel).add(username);
String message = "User " + username + " just entered the channel";
notifyChannelSystem(channel, "SYSTEM", message);
System.out.println("User " + username + " connected to channel " + channel + " which was already created by someone else");
}
}
}
updateJListForActiveChannels();
updateJListForUsersInChannel();
}
@Override
public ArrayList<String> getUsersInChannel(String channel) throws RemoteException{
return channels.get(channel);
}
@Override
public void updateJListForUsersInChannel() throws RemoteException{
for(Client c : clients){
c.getJListForUsersInChannel();
}
}
GUILogic:
DefaultListModel usersInChanDLM = new DefaultListModel();
public ArrayList<String> getUsersInChannel(String channel) throws RemoteException{
return cf.getUsersInChannel(channel);
}
public DefaultListModel getUsersInChannelAsDefaultListModel(String channel) throws RemoteException{
if(!(getChannel() == null)){
for(String a : cf.getUsersInChannel(getChannel())){
usersInChanDLM.addElement(a);
}
}
return usersInChanDLM;
}
void updateUsersInChannelJlist(JList jList3) throws RemoteException {
usersInChanDLM.clear();
for(Client c : cf.getClients()){
if(!(usersInChanDLM.contains(c.findName()))){
usersInChanDLM.addElement(c.findName());
}
}
jList3.setModel(usersInChanDLM);
}
GUI: jList:
usersInChannelJList = new javax.swing.JList();
try{
usersInChannelJList.setModel(gl.getUsersInChannelAsDefaultListModel(gl.getChannel()));
usersInChannelJList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
}catch(RemoteException ex){
System.out.println(ex);
}
jScrollPane4.setViewportView(usersInChannelJList);
接口:
服务器:
void updateJListForUsersInChannel() throws RemoteException;
客户端:
void getJListForUsersInChannel() throws RemoteException;
**
**
JList显示当前连接的所有用户,我猜这是因为这个方法
public ArrayList<String> getUsersInChannel(String channel) throws RemoteException{
return channels.get(channel);
}
我想要做的是从所选频道获取包含所有用户的数组。有没有简单的方法可以从频道哈希表中做到这一点?