Results 1 to 4 of 4
- 01-18-2012, 04:41 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
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:
The GUI Logic:Java Code:ArrayList clients = new ArrayList<Client>(); public ArrayList<Client> getClients(){ return clients; }
The GUI:Java 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(); }
Java 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?)
The button which runs the updatelistmethod:Java 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); }
Java 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); } }
- 01-18-2012, 04:51 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
- 01-18-2012, 05:02 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
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?
- 01-18-2012, 05:51 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Similar Threads
-
adding drawing images on a "Graphics instance"
By imorio in forum New To JavaReplies: 1Last Post: 02-19-2011, 10:30 PM -
Adding "Choose File" button to applet
By mju4t in forum Java AppletsReplies: 1Last Post: 09-17-2009, 08:11 AM -
jList Issues - getSelectedValue() Keeps returning "null"
By MoobKeeng in forum New To JavaReplies: 0Last Post: 07-28-2009, 06:45 PM -
Listener for action "team->commit"
By feva in forum EclipseReplies: 0Last Post: 07-27-2009, 07:40 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks