0

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.

Lost Sorcerer
  • 905
  • 2
  • 13
  • 26
  • "if I display the image in a JOptionPane then add it to the JPanel the image is displayed". What is wrong here? – Nikolay Kuznetsov Jan 10 '13 at 12:12
  • The only way the image gets shown on the GUI is if it has been displayed in a JOptionPane before it is added to the JPanel. The issue is, I have to display the image before I can display the image. – Lost Sorcerer Jan 10 '13 at 12:18
  • you mean without `JOptionPane.showMessageDialog(null, imgLabel1,"Label",-1);` this line `jpnDisplay.add(imgLabel1);` would have no effect? – Nikolay Kuznetsov Jan 10 '13 at 12:30
  • yes. That is the part that makes this even more confusing. – Lost Sorcerer Jan 10 '13 at 12:40
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jan 10 '13 at 12:40
  • @AndrewThompson I am not sure how to do a proper SSCCE and unsure if that would help. I posted in my question the exact lines that caused the issue. And based on what I skimmed from the link, this would be close without posting the files(as you could use any BufferedImage) – Lost Sorcerer Jan 10 '13 at 12:44
  • *"unsure if that would help"* So don't try & prepare and post SSCCE. Remain in the dark, and ignored, as is your right. I don't understand your last sentence. For the only thing I can interpret it as.. hot-link to an image. – Andrew Thompson Jan 10 '13 at 12:49
  • Sorry if I was not clear: Beyond loading the image from a file, then creating a subimage from it there is no other code in my project. And about the last part, if you wanted to test my code you could copy-paste it and load your own BufferedImage to use. – Lost Sorcerer Jan 10 '13 at 12:55
  • And about not wanting to learn SSCCE, Currently it would not serve me in my current standing as a student. The programs I currently end up writing are simple enough to isolate the problem code. – Lost Sorcerer Jan 10 '13 at 12:57
  • @RMDan : I hope you are starting your Swing Application on the EDT - [Initial Threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). Else hopefully this [answer](http://stackoverflow.com/a/11372350/1057230), might help you sort the thingy out. – nIcE cOw Jan 10 '13 at 20:34

1 Answers1

1

You must subclass your JPanel and override the redraw method to draw you image.

Sporniket
  • 339
  • 1
  • 4
  • Could you provide some details? I am not familiar with the drawing methods in Java. My college classes mainly focused on data structures and now Remote communication. – Lost Sorcerer Jan 10 '13 at 13:18
  • check this tutorial : http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html then use drawImage of the Graphics2D provided by redraw (you will need to cast the Graphics into Graphics2D) – Sporniket Jan 10 '13 at 13:23
  • Thank you for the tutorial. This will help greatly. Also I edited your answer to include the link for clarity. – Lost Sorcerer Jan 10 '13 at 13:30
  • The tutorial is great but it turns out it did not solve my issue. It did clear up what the issue was. It was not my lack of skill, but something else. Subclassing JPanel did not work unless I did a JOptionPane first(same as with JLabel). The reason why is still unclear – Lost Sorcerer Jan 10 '13 at 14:02
  • Is there any call to the panel method `repaint()` or `invalidate()` after setting the Image ? – Sporniket Jan 10 '13 at 14:24
  • Neither method call makes a difference. Your idea works if I manual code a frame, but using a editor built one and adding a panel to it does not work(cept in strange circumstances) – Lost Sorcerer Jan 10 '13 at 14:38
  • hum, I thought you was manually coding your UI. Then their must be other step to follow to trigger the redraw. Did you call invalidate/repaint from outside the subclass or inside ? (i.e., does your code look like `myPanel.setImage(myImage);myPanel.invalidate()` or like `myPanelClass extends JPanel{ ... public void setImage(Image newImage){myImage = newImage;invalidate();} }` ? I believe the latter should work) – Sporniket Jan 11 '13 at 12:58