Problems updating my JList for active channels

2019-06-09 15:10发布

Intro:

I've created a chatprogram where a client connects the server. The client is able to join channels, and when they do, the jList(see code) should be updated via DefaultListModel with (getChannel()), and should show all other clients connected to the same channel. I know the getChannel(); works, since ive debugged it(and use systemoutprint). I already have Jlists for active channels and usersonline, which works! :)

The clients use callback methods to notify the other clients connected to the server.

I've done some debugging, and i've been trying going over my code ton of times.

Heres the code:

Server:

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: The 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);

Interfaces:

Server:

    void updateJListForUsersInChannel() throws RemoteException;

Client:

    void getJListForUsersInChannel() throws RemoteException;

**

Problem:

**

The JList is showing all users connected at the moment, which i guess is because of this method

public ArrayList<String> getUsersInChannel(String channel) throws RemoteException{
        return channels.get(channel);   
    }

what i want to do is get an array with all users from the selected channel. Is there any easy way to do that from the channels hashtable?

0条回答
登录 后发表回答