Okay, so say my code looks like this:
private void gameBoxActionPerformed(java.awt.event.ActionEvent evt) {
gameBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
try {
if(gameBox.getSelectedItem().equals("file1"))
gameLabel.setIcon(new ImageIcon(ImageIO.read(new File("src/icons/file1.png"))));
if(gameBox.getSelectedItem().equals("file2"))
gameLabel.setIcon(new ImageIcon(ImageIO.read(new File("src/icons/file2.png"))));
if(gameBox.getSelectedItem().equals("file3"))
gameLabel.setIcon(new ImageIcon((ImageIO.read(new File("src/icons/file3.png")))));
if(gameBox.getSelectedItem().equals("file4"))
gameLabel.setIcon(new ImageIcon(ImageIO.read(new File("src/icons/file4.png"))));
if(gameBox.getSelectedItem().equals("file5"))
gameLabel.setIcon(new ImageIcon(ImageIO.read(new File("src/icons/file5.png"))));
if(gameBox.getSelectedItem().equals("file6"))
gameLabel.setIcon(new ImageIcon(ImageIO.read(new File("src/icons/file6.png"))));
} catch (IOException ex) {
Logger.getLogger(GUIMain.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
And the starting icon for gameLabel
is file1.png
and the desired action upon changing the item in gameBox
(which is a combobox) is the imageIcon
for gameLabel
changing.
The problem I'm having, is it won't change until two selections are made inside gameBox
After that it works perfectly.
How do I set it so that it changes on the first change? And why is it not doing that already?