Hi all,
I am trying to do a simple combobox which according to what is selected an image is shown. This is working however I have to minimize and maximize the window so that the image is shown. Moreover if 2 different choices are done on the same run both pics are being shown on the panel. Can you please help?
Here is the code:
import javax.swing.*; import java.awt.event.*; public class Images extends JFrame implements ActionListener { String[] formats = {"Green","Red"}; JComboBox formatBox = new JComboBox(); JPanel pane = new JPanel(); JLabel formatLabel; public Images() { super("Door Choice"); setSize(220, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); formatLabel = new JLabel("Choose door type:"); pane.add(formatLabel); for (int i = 0; i < formats.length; i++){ formatBox.addItem(formats[i]); } pane.add(formatBox); formatBox.addActionListener(this); add(pane); setVisible(true); } public void actionPerformed(ActionEvent ae) { ImageIcon myDoor= new ImageIcon("C:\\Users\\Projector\\Desktop\\Doors\\"+formatBox.getSelectedItem()+".png"); JLabel myImage = new JLabel(); myImage.setIcon(myDoor); pane.add(myImage); } public static void main(String[] args) { Images image = new Images(); } }