Java JList refresh only after minimized or maximized
I have a JList
connected to a collection.
When collection change, I need the JList
to be automatically refreshed. But I can see that my JList
will refresh only after I minimized (or maximized) the JFrame
.
Why?
I'm new with Java and I'm trying to learn.
I have this collection for the list model:
public class UserCollection extends Vector<User> implements ListModel{
private static final long serialVersionUID = 2668410577023194442L;
@Override
public void addListDataListener(ListDataListener arg0) {
System.out.println("add list! --- " +arg0.toString());
}
@Override
public Object getElementAt(int index) {
return this.get(index).getName();
}
@Override
public int getSize() {
return(this.elementCount);
}
@Override
public void removeListDataListener(ListDataListener arg0) {}
}
In another class, I populate the coll开发者_高级运维ection with userList.add(u1);
or userList.remove(u1);
In the JFrame
class I have:
JList list = new JList();
list.setModel(xmppManager.userList);
I can see on console the collection changing, and if I minimize the JFrame
and/or maximize, the JList
is refreshed properly...
If you can, I suggest you use a DefaultListModel as your JList's model. This model will automate the change of the view (the JList) as the model changes and will make your life much easier. If you absolutely must use a collection of your own making, then see if you can have the class that holds it extend an AbstractListModel. If you do this, be sure to call the appropriate fireXXXX() method whenever you change data in the model.
Once your collection has changed, call a refresh on your JList:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jlist.revalidate(); // triggers a repaint of all the items in the JList.
jlistContainer.repaint(); // Not sure if this one is needed
}
});
How your JList model is updated?
[EDIT] Now that we have your code, you should look at AbstractListModel and implements your model in the same way, or better, extends AbstractListModel.
Actually, you add data to your collection but the model is not notified of this change!
When you added or removed items using the add and remove methods, you were calling methods in Vector. This updated your model but the actual JList had no way to know that the model was updated and that it needed to refresh itself.
(I think others have adequately explained how to fix the problem)
Finally I have resolved this thanks to your help!
This is what I did:
1) In the class doing the work I have
public DefaultListModel userList;
and I populate it with userList.addElement(user)
and userList.removeElement(user)
2) in the JFrame
class, I have:
JList list = new JList();
list.setCellRenderer(new UsersRenderer());
list.setModel(xmppManager.userList);
3) and this is my UsersRenderer
:
import java.awt.Color;
import java.awt.Component;
import javax.swing.*;
public class UsersRenderer extends javax.swing.JPanel implements ListCellRenderer {
/**
*
*/
private static final long serialVersionUID = -9210143012283239644L;
public UsersRenderer() {
initComponents();
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
User person = (User) value;
labelUser.setText(person.getName() + " " + person.getAddress());
if (isSelected) {
setBackground(Color.red);
setForeground(Color.white);
} else {
setBackground(Color.white);
setForeground(Color.black);
}
return this;
}
private void initComponents() {
labelUser = new javax.swing.JLabel();
labelUser.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
labelUser.setText("testo");
add(labelUser);
}
private javax.swing.JLabel labelUser;
}
精彩评论