I am trying to display an image in a JFrame
GUI in Java. I have successfully loaded the image from a resource file and been able to display the image in a JOptionPane
. This was achieved using a JLabel
containing a Image Icon in the constructor. When trying to add this image to a JPanel
nothing is displayed.
JLabel imgLabel1 = new JLabel(new ImageIcon(tsr.getTileImage(1,1)),JLabel.CENTER);
jpnDisplay.add(imgLabel1);
tsr is my custom code for getting a subimage from a tileset. The image returned is of type BufferedImage
.
One thing I did notice is if I display the image in a JOptionPane
then add it to the JPanel
the image is displayed. I am unsure why this is.
JLabel imgLabel1 = new JLabel(new ImageIcon(tsr.getTileImage(1,1)),JLabel.CENTER);
JOptionPane.showMessageDialog(null, imgLabel1,"Label",-1);
jpnDisplay.add(imgLabel1);`enter code here
--EDIT--
After playing around with my code, I have discovered my issue was not with the way I was trying to display the images, but that for some reason my JFrame
was not repainting unless a JOptionPane
was shown before the JFrame
was shown. It also only paints the same instance that was shown in the JOptionPane
. Any other images to be painted get ignored. The reason is unclear.