-1

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Jan 11 '15 at 11:43
  • 2
    Also consider a `Map` to associate names. – trashgod Jan 11 '15 at 11:45
  • 1
    @trashgod Good point, but in this case where the action command seems to map exactly to the name of the resource, I'd be tempted to try something like `String name = gameBox.getSelectedItem(); URL url = this.getClass().getResource("/icons/ + name); gameLabel.setIcon(..`. But of course since the images should be loaded on construction and cached, your suggestion of a `Map` (or more specifically a **`Map`**) to store/retrieve them makes more sense. – Andrew Thompson Jan 11 '15 at 11:49
  • 1
    *"The problem I'm having, is it won't change until two selections are made inside `gameBox` After that it works perfectly."* 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) One way to get images for an example, is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jan 11 '15 at 11:53
  • It looks like `gameBoxActionPerformed` is your event handler (where would you get the `ActionEvent` parameter from otherwise?) Inside this handler you seem to install *another* actionlistener. Can you show the code that invokes/references the `gameBoxActionPerformed`. I would expect the `actionPerformed` code in the example above to be sitting directly under `gameBoxActionPerformed`, rather than nested in another action handler. – geert3 Jan 11 '15 at 13:23

1 Answers1

-1

I fixed it.

As per mention from geert3 I had nested action listeners, and they were causing issues.

Solution in short:

        if(gameBox.getSelectedItem().equals("file1"))
            gameLabel.setIcon((Icon) new ImageIcon("src/icons/file1.png"));
                repaint();

Rather than:

       if(gameBox.getSelectedItem().equals("file1"))
                gameLabel.setIcon(new ImageIcon(ImageIO.read(new File("src/icons/file1.png"))));

Resulted in exactly what I wanted.