Results 1 to 3 of 3
  1. #1
    Fleur is offline Member
    Join Date
    Apr 2008
    Posts
    19
    Rep Power
    0

    Default 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?

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Show the code you have so far.

  3. #3
    Fleur is offline Member
    Join Date
    Apr 2008
    Posts
    19
    Rep Power
    0

    Default

    This is what i have so far:
    Java Code:
                      try {
                          FileOutputStream oFile = new FileOutputStream("photo.jpg");
                          oFile.write(Picture.getData());
                          oFile.close();
                      }catch (Exception e){}
    and a bit further on:

    Java Code:
            ShowImage photo = new ShowImage();
            photo.setBounds(280,20,200,350);
            this.getContentPane().add(photo);
            this.repaint();
    where ShowImage extends JPanel:

    Java 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 :)

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  2. Replies: 2
    Last Post: 04-07-2009, 06:25 AM
  3. [SOLVED] File chooser selecting file from directory...?
    By prabhurangan in forum AWT / Swing
    Replies: 12
    Last Post: 06-18-2008, 04:08 AM
  4. Replies: 7
    Last Post: 05-23-2008, 03:46 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •