XliteRSPS
XliteRSPS

Reputation: 106

Inserting an Image into a JList

I am trying to load an Image into a List but I can't seem to get it to work. I have tried

ImageIcon pic = new ImageIcon("http://i3.ytimg.com/vi/68X8RUxeXeA/default.jpg");
JLabel picLbl = new JLabel(pic);

Object[] lol = {picLbl, "pic", "length"};
list = new JList(lol);
scrollPane.setViewportView(list);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

That just puts javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=http://i3.ytimg.com/vi/68X8RUxeXeA/default.jpg,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]

Instead of the actual picture. I have looked around for a while but can't seem to find anything that works. If any of you guys know how to achieve this, any help will be greatly appreciated.

Thanks in advance

Upvotes: 1

Views: 1780

Answers (2)

trashgod
trashgod

Reputation: 205775

A DefaultListCellRenderer is a JLabel, so you can use setIcon() with an ImageIcon. This related example may be a useful starting point.

Upvotes: 2

Jakub Zaverka
Jakub Zaverka

Reputation: 8874

Jlabels cannot be displayed by JList. Create an ImageIcon from your Image and use that for your array of elements.

If you want to know more about how a JList actually displays elements, read http://docs.oracle.com/javase/tutorial/uiswing/components/list.html#renderer

Upvotes: 2

Related Questions