2
public String[] imagesArray = {Images.firstImage, Images.secondImage};

String imagesPath = "/testproject/images/";
 for(int i = 0; i<imagesArray.length; i++) {
    URL imageURL = this.getClass().getResource(imagesPath+imagesArray[i]);
    ImageIcon orignalImageIcon = new ImageIcon(imageURL);
    Image newImage = orignalImageIcon.getImage().getScaledInstance(100, 90, java.awt.Image.SCALE_SMOOTH);
    ImageIcon newImageIcon = new ImageIcon(newImage);

    JButton receiptButton = new JButton(newImageIcon);
    receiptButton.setBorder((new EmptyBorder(0,0,0,0)));
    toolBar.add(receiptButton);
    add(toolBar);
    }

Images not shown in my design layout?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Faizan Ahmed
  • 93
  • 2
  • 4

1 Answers1

4

The problem is most likely the asynchronous loading nature of using an ImageIcon to load the original images.

If that is the problem:

  1. There is an easy way to test it. Add the orignalImageIcon to the button and see if they all appear.
  2. There is an easy way to fix it. Load the images using ImageIO.read(URL) - a method that will block until the image is completely loaded.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Can you give me an example how i would load images using ImagesIO.read(URL) ? – Faizan Ahmed Sep 04 '12 at 19:29
  • 2
    @FaizanAhmed: Here's one [example](http://stackoverflow.com/a/2658663/230513) and another [example](http://stackoverflow.com/a/6296381/230513). +1, btw. – trashgod Sep 04 '12 at 19:50