0

Possible Duplicate:
Java:Add an image to a JList item

public class ClientUserPanel extends JPanel {

    private JList userlist;
    private JScrollPane scrollist;
    private DefaultListModel listmodel;

    public ClientUserPanel() {
        super();
        init();
    }

    private void init() {
        this.setLayout(new BorderLayout());
        this.setSize(100, 200);
        listmodel = new DefaultListModel();
        userlist = new JList(listmodel);
        scrollist = new JScrollPane(userlist);
        listmodel.addElement("uday");
        listmodel.addElement("ravi");
        listmodel.addElement("uday");
        this.add(scrollist, BorderLayout.CENTER);
    }
}

Here I have a JList and i had added items to the list and its working correctly, but i want to add an icon in JList beside each item . for example to show the status of user,either he was offline or online. can any one please help me

Community
  • 1
  • 1
uday gowda
  • 609
  • 3
  • 10
  • 16

1 Answers1

1

You need to implement your own ListCellRenderer. This will allow you to define how each element should be painted. You can also refer to here for more information and examples of how to achieve it.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Good answer to a duplicate question, but please consider linking to the latest (v. 7) JavaDocs in future. – Andrew Thompson Jul 20 '12 at 01:34
  • Not every one's using Java 7 ;), but I personally have no issues – MadProgrammer Jul 20 '12 at 02:10
  • (shrugs) I'm coding for v. 6 at the moment, but *always* use the latest JavaDocs. That way, I can get a 'heads up' on methods, attributes or classes that might be deprecated in Java 7. If the replacement is not available in Java 6, I'll add a TODO flag to the (soon to be) deprecated code. I feel it is best to link to the latest version of the JavaDocs. For tips on getting a link to the latest docs, see [point 2 of advantages](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7090875). Please vote for the RFE. I raised the RFE - so yes, this is something I **have** thought about. ;) – Andrew Thompson Jul 20 '12 at 03:09