1

I have created an array for Java with what I believe is the correct method, however, when I attempt to display the image in a button, nothing happens and I am unable to find what the reason behind it is (code updated thanks to Frakcool

private void showOnesSecsActionPerformed(java.awt.event.ActionEvent evt) {                                             

    ImageIcon[] secs;
    secs = new ImageIcon[10]; Integer.parseInt(oneSecs.getText());
    for (int i = 0; i < 10; i++)
    {
        String location = "images\\" + i + ".png";
        secs[i] = new ImageIcon(location);
        oneSecsDisplay.setIcon(secs[i]);
    }

}

In the above snippet, the image is called from the secs[i] variable and then set as oneSecsDisplay new image.

oneSecsDisplay is a button that will show the image once another button called showOneSecs is pressed

I have 10 images from 0 to 9 that need to be displayed as a button is pressed; I was given the code:

int ones = Integer.parseInt(oneSecs.getText());
if (ones == 0) oneSecsDisplay.setIcon(new javax.swing.ImageIcon(filelocation);
if (ones == 0) oneSecsDisplay.setIcon(new javax.swing.ImageIcon(filelocation);

I don't think that this is good practice at it will be too repetitive and rather messy.

11
  • Maybe your path is wrong. Are they PNG pics, instead of JPG? Try changing String location = "\\images\\" + i + ".png"; for String location = "/images/" + i + ".png"; or for this one: String location = "images/" + i + ".png";. And if you have 10 images your for loop will only bring from 0-8 (i.e. only 9), change it to for (int i = 0; i <= 9; i++) or for (int i = 0; i < 10; i++) so it brings 10 images instead of 9. Hope it helps. Good Luck Commented Oct 30, 2014 at 17:26
  • I checked to make sure, and you were right about the path being wrong, I forgot to move the folder from the src folder into the root, thank you for that! :) However the only image that shows is the image for number 9, from 0 to 8 all numbers show as 9 Commented Oct 30, 2014 at 17:31
  • 1
    Can you post an MCVE? Please or at least a bit more code? I guess your image 9 is shown because of another for loop somewhere else. And I also checked this line: secs = new ImageIcon[9]; that will overflow as you have 10 images and it will store only 9. Commented Oct 30, 2014 at 17:45
  • added more code to the original post :) Commented Oct 30, 2014 at 18:21
  • 1
    Where in your code do you add the image to the button? Please again, I suggest you to read the link I provided about How to make a Minimal, Complete and Verifiable Example (MCVE). Help us to help you :) Btw what does oneSecsDisplay is? Commented Oct 31, 2014 at 3:22

1 Answer 1

2

If you are trying to set just the correct image, there is no need for arrays or for loops. Try this instead:

private void showOnesSecsActionPerformed(java.awt.event.ActionEvent evt) {
    int index = Integer.parseInt(oneSecs.getText());
    String location = "images\\" + index + ".png";
    oneSecsDisplay.setIcon(new ImageIcon(location));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, this code is perfect! Was wracking my brain on how to make this easier haha!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.