Adding a fire listener to a JList which "has" an DefaultListModel.
Hi. First post here! yeey!
I'm trying to figure out how i can update my JList with users on a server automaticly when someone enters the server. The server\client is RMI with chatrooms and such. The chat etc is working great, but now i've made a list which i can update with a button, which gets the newest content from the arraylist.
This is how i've done it:
The serverside:
Code:
ArrayList clients = new ArrayList<Client>();
public ArrayList<Client> getClients(){
return clients;
}
The GUI Logic:
Code:
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();
}
The GUI:
Code:
jList2 = new javax.swing.JList();
try{
jList2.setModel(gl.getUsersOnlineAsDefaultListModel(gl.getClients())
);
}catch(RemoteException ex){
System.out.println(ex);
}
jScrollPane3.setViewportView(jList2);
The update method(can this be used with a eventlistener?)
Code:
public void UpdateJList(JList jlist) throws RemoteException{
for(Client c : cf.getClients()){
if(!(result.contains(c.findName()))){
result.addElement(c.findName());
}
}
jlist.setModel(result);
jlist.setSelectedIndex(0);
}
The button which runs the updatelistmethod:
Code:
private void updateListButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
allOnlineList.clearSelection();
gl.UpdateClientsJList(allOnlineList);
allOnlineList.updateUI();
} catch (RemoteException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
Re: Adding a fire listener to a JList which "has" an DefaultListModel.
Does it work?
I will say the RMI call should probably be on its own thread (see SwingWorker), so you don't lock up the GUI while waiting for the server to respond.
Re: Adding a fire listener to a JList which "has" an DefaultListModel.
Yes, the updatemethod does work, but its not very convenient usage for the user tho ^^ The thread thingy doesn't seem like a problem yet, and at the moment i do't want to complicate things too much i think.
http://docs.oracle.com/javase/tutori...alistener.html
Isn't this exactly what i need?
Re: Adding a fire listener to a JList which "has" an DefaultListModel.
Looks like it could be a solution.