6

I have a JList and i am using DefaultListModel,everything is well and the items(strings)are added correctly, but i want to add an image in JList beside each string (e.g.to show the status of the users).Can anyone help me about that? Thanks in advance.Here is how i add the elements,can i add images too?

private  DefaultListModel modelO = (DefaultListModel) Teacher.made_list.getModel();
((DefaultListModel) Teacher.made_list.getModel()).addElement(studName);
Lesmana
  • 25,663
  • 9
  • 82
  • 87
Fatema Mohsen
  • 175
  • 2
  • 5
  • 11
  • 5
    Accept an answer so that people can know which solution solved your problem. – prolink007 Aug 26 '11 at 01:33
  • Accept the answer, please! Mark the `V` at the left side of the question and it will become green meaning this was the solution that worked for you. If you're not fully satisfied with the answers, please post a comment on it saying what is missing to become acceptable by you. – Math Oct 11 '13 at 13:32

3 Answers3

7

You have to implement ListCellRenderer (or extend DefaultListCellRenderer) and have the getListCellRendererComponent method to return a Jlabel with an icon in it.

Example:

public class IconListRenderer extends DefaultListCellRenderer {
    public Component getListCellRendererComponent(
            JList list, Object value, int index,
            boolean isSelected, boolean cellHasFocus) {
        JLabel label = (JLabel) super.getListCellRendererComponent(
                list, value, index, isSelected, cellHasFocus);
        Icon icon = this.getIcon(list, value, index, isSelected, cellHasFocus)
        label.setIcon(icon);
        return label;
    }
    protected Icon getIcon(
            JList list, Object value, int index,
            boolean isSelected, boolean cellHasFocus) {
        // how do I get icon?
    }
}

You have to implement the getIcon method.

Lesmana
  • 25,663
  • 9
  • 82
  • 87
3

The model is used to store the data, and a renderer is used to display the data. The default renderer can handle Strings and icons but if you need to do more than that, you can provide a custom renderer. Here is an example. It's for a combo box, but the renderer is the same for JLists.

z7sg Ѫ
  • 3,153
  • 1
  • 23
  • 35
  • +1 for the link to the Swing tutorial. Tt helps solve this problem and the tutorial can be used as a resource for future questions. – camickr Apr 27 '11 at 17:39
  • 2
    @Fatema Mohsen you are welcome :) but you don't really need to thank us, just an upvote and marking one of the answers correct will make everyone happy. – z7sg Ѫ Apr 28 '11 at 11:32
  • To clarify, "default renderer can handle Strings and icons" means that it can handle *either* a string or an icon but not both at the same time. At least that is how it worked for me. – Bijou Trouvaille May 30 '14 at 05:50
0

Here you can find complex solution including

  • Custom JList elements (that allows to get icon image from list element)
  • Handling selection styling
  • More convinient way of using ListCellRenderer than just extending Default

http://www.codejava.net/java-se/swing/jlist-custom-renderer-example

ANTARA
  • 810
  • 1
  • 13
  • 20