-
waiting for a file
Hi people,
I'm trying to get a picture shown in a JPanel.
The problem is that the picture gets created when the app starts. This takes a while. Apparently the picture is not yet created when the app gets to the code to show the (not yet existing) picture which obviously generates an error.
How do I handle this?
-
Show the code you have so far.
-
This is what i have so far:
Code:
try {
FileOutputStream oFile = new FileOutputStream("photo.jpg");
oFile.write(Picture.getData());
oFile.close();
}catch (Exception e){}
and a bit further on:
Code:
ShowImage photo = new ShowImage();
photo.setBounds(280,20,200,350);
this.getContentPane().add(photo);
this.repaint();
where ShowImage extends JPanel:
Code:
public class ShowImage extends JPanel {
BufferedImage image;
public ShowImage() {
try {
File input = new File("photo.jpg");
image = ImageIO.read(input);
} catch (Exception ie) {}
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, null);
}
It will do what I want the second time I run the app: the phot.jpg file is already there from the previous time and the picture will load into the ShowImage, but clearly I don't need the picture of the previous run :)