What I want to do is to display an image when a specified item is selected from the comboBox, knowing that the items of the comboBox change and the number is not fix. This code shows how I add the items which depends on my "for"
for (int j = 1; j <= 6; j++) {
if (condition) {
System.out.println(result);
combo.addItem("component N°" + j);
}
}
What I need is to display a certain image when a an item is selected! I really don't know how to do it. I've tried actionperformed with actionlistener, but I didn't know how to relate the item choosing with my images.
I use this method to display an image into JPanel
public static void display(String path, JPanel panel) {
BufferedImage image = null;
try {
image = ImageIO.read(new File(path));
} catch (IOException e2) {
e2.printStackTrace();
}
Image dimg = image.getScaledInstance(panel.getWidth(), panel.getHeight(),
Image.SCALE_SMOOTH);
panel.add(new JLabel(new ImageIcon(dimg)));
}