You must restructure your class so that all MyRender children are created and added at build time.
getListCellRendererComponent() should be used ONLY to change the values ββor visual attributes (e.g. background) of existing components.
Do not forget that getListCellRendererComponent() should be as fast as possible (it can be called quite often), so it should not create components, but only modify existing ones.
Typically, this is what your getListCellRendererComponent() method looks like:
@Override public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { if (value != null) { User user = (User) value; String pres = user.getPresence().toLowerCase(); img.setIcon(default_img); if (pres.contains("unavailable")) icn.setIcon(off_img); else icn.setIcon(on_img); name.setText(user.getName()); if (isSelected) { setBackground(Color.lightGray); panel.setBackground(Color.lightGray); } else { setBackground(Color.white); panel.setBackground(Color.white); } } return this; }
source share