Results 1 to 4 of 4
  1. #1
    sindrem is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default 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:

    Java Code:
    ArrayList clients = new ArrayList<Client>();
    
    public ArrayList<Client> getClients(){
        return clients;          
    }
    The GUI Logic:

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


    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?)

    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);
    }
    The button which runs the updatelistmethod:

    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);
            }
        }

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default 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.

  3. #3
    sindrem is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default 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?

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Adding a fire listener to a JList which "has" an DefaultListModel.

    Looks like it could be a solution.

Similar Threads

  1. adding drawing images on a "Graphics instance"
    By imorio in forum New To Java
    Replies: 1
    Last Post: 02-19-2011, 10:30 PM
  2. Adding "Choose File" button to applet
    By mju4t in forum Java Applets
    Replies: 1
    Last Post: 09-17-2009, 08:11 AM
  3. Replies: 0
    Last Post: 07-28-2009, 06:45 PM
  4. Listener for action "team->commit"
    By feva in forum Eclipse
    Replies: 0
    Last Post: 07-27-2009, 07:40 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •