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.
String location = "\\images\\" + i + ".png";forString 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 tofor (int i = 0; i <= 9; i++)orfor (int i = 0; i < 10; i++)so it brings 10 images instead of 9. Hope it helps. Good Lucksecs = new ImageIcon[9];that will overflow as you have 10 images and it will store only 9.oneSecsDisplayis?