Question

How can I display jpg image which I stored in arraylist in JPanel? Im not able to display the jpg files in the JPanel.

String[] pictureFile   = {"A.jpg","B.jpg","C.jpg"};
List<String>  picList1 = Arrays.asList(pictureFile);

Collections.shuffle(picList1); 

ImageIcon icon = new ImageIcon("picList1.get(0)");
JLabel label1   = new JLabel();
label1.setIcon(icon);

JPanel panel = newJPanel;
panel.add(label); 
Was it helpful?

Solution

You should not put the call to the array in quotes.

Instead, you should try the following:

ImageIcon icon = new ImageIcon(picList1.get(0));

OTHER TIPS

The problem is in the line

ImageIcon icon = new ImageIcon("picList1.get(0)");

It's interpreting the string as a file name. You should just need to unquote the picList1.get(0) bit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top