Question

i know this question asked many times before. However, i spent on hours to research about a way to update a JList, and i couldn't handle with this issue. I'm trying to implement a JList which shows who is online or offline.(It holds JLabels i guess this is the only way for holding a string with an icon). I have my own CellRenderer() which is like;

public class UserRenderer extends DefaultListCellRenderer {


    @Override
    public Component getListCellRendererComponent(JList list, Object value,int index, boolean isSelected, boolean hasFocus) {

       if(value instanceof ClientObject){

           final ClientObject clientObject = (ClientObject) value;
           JLabel label = (JLabel) super.getListCellRendererComponent(list, clientObject.getNickName(), index, isSelected, hasFocus);

           if(clientObject.isIsOnline()){

               label.setIcon(iconArray[1]);
           }
           else{

               label.setIcon(iconArray[0]);
           }

           return label;
       }

       else {

           return super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
       }
    }

}

Moreover, i construct client's list whenever he/she gets connected to server with this way;

private void buildBuddyList(ClientObject tempClientObject){

    if( tempClientObject.getBuddyList().size() > 0 ){

        mainClient.setBuddyList(tempClientObject.getBuddyList());

        for (Iterator<ClientObject> iter = mainClient.getBuddyList().iterator(); iter.hasNext();) {

            ClientObject tempon = iter.next();

                if(tempon.isIsOnline()){

                    model.addElement(tempon);
                    labelIconList.put(tempon, iconArray[1]);
                }

                else{

                    model.addElement(tempon);
                    labelIconList.put(tempon, iconArray[0]);
                }
        }
    }
}

The trick which i use when a client changed his/her status (becomes online/offline) is i get rid off all elements with model.clear() and start to construct the list again. Here is the code segment;

       if(tempClientObject.isStatusChanged()){

          if(tempClientObject.isIsConnected()){ 

                System.out.println(tempClientObject.getUserName() + " is ONLINE");

                model.clear();

                for (Iterator<Map.Entry<ClientObject,ImageIcon>> iter = labelIconList.entrySet().iterator(); iter.hasNext();) {

                    Map.Entry<ClientObject,ImageIcon> pairs = iter.next();

                    ClientObject changedOnlineStatusClient = (ClientObject) pairs.getKey();

                    if(changedOnlineStatusClient.getUserName().equals(tempClientObject.getUserName())){

                        changedOnlineStatusClient.setIsOnline(tempClientObject.isIsOnline());
                    }

                    model.addElement(changedOnlineStatusClient);
                }
          }

          else{

                System.out.println(tempClientObject.getUserName() + " is OFFLINE");                

                model.clear();

                for (Iterator<Map.Entry<ClientObject,ImageIcon>> iter = labelIconList.entrySet().iterator(); iter.hasNext();) {
                    Map.Entry<ClientObject,ImageIcon> pairs = iter.next();
                    ClientObject changedOnlineStatusClient = (ClientObject) pairs.getKey();

                    if(changedOnlineStatusClient.getUserName().equalsIgnoreCase(tempClientObject.getUserName())){

                        changedOnlineStatusClient.setIsOnline(tempClientObject.isIsOnline());
                    }

                    model.addElement(changedOnlineStatusClient);
                }                      
          }
       }

I can assure that logical system works fine.(I can check the actions if they are working properly on background). Only the problem is sometimes when a client connected to server JList looks blank even though it adds elements into model. i will appreciate for every answer. And thanks anyway

Was it helpful?

Solution 2

First of all thanks for all your replies. On the other hand, i corrected my issue with creating a new model whenever it has to be get updated. So the code segment is like this;

DefaultListModel tempModel = new DefaultListModel();

// add or remove elements from tempModel

buddyList.setModel( tempModel );

i'm not sure about if it is the only correct way but at least it works.

OTHER TIPS

only comment, not an answer

there no require for one dimensional JList

if(value instanceof ClientObject){

because Object from

getListCellRendererComponent(JList list, Object value, int index, 
       boolean isSelected, boolean hasFocus) {

returns the same value, then to test this value if == or equeals ...

Call model.fireContentsChanged() method.

JavaDoc on AbstractListModel

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top