title: How to capture image from a jframe?(too fast)
body: <p>I have successfully done this before but now after running the program numerous times the failure comes at the screenshot is too fast. And it won't take the correct image it takes it as the jframe is fading in. <strong>How do I fix this?</strong><img src="https://i.stack.imgur.com/AxWF3.png" alt="Error"></p>
  
  <p><strong>Edit :</strong> So basically I'm trying to capture the image of the Jpanels inside the JFrame but in the process of doing this by calling it out of an excel macro the image captured turns out to be the one above instead of the image I need. </p>
  
  <pre><code>import java.io.*;
  import javax.swing.*;
  import java.awt.event.*;
  import javax.imageio.ImageIO;
  import java.awt.image.BufferedImage;
  public class TimeTableGraphicsRunner extends JFrame
  {
      public TimeTableGraphicsRunner()
      {
          getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
      }
      public void load(TimeTablePanel pan)
      {
          pan.setTitle(x.getTitle(TimeTable.titleCount));
          getContentPane().add(pan);
      }
      public void run()throws Exception
      {
          System.out.println("Creating image in panel");
          //setSize(TimeTable.width,TimeTable.height);
          getContentPane().setPreferredSize(new java.awt.Dimension(TimeTable.width,TimeTable.height));
          pack();
          setVisible(true);
          System.out.println("Image is in panel");
          grabScreenShot();
          System.out.println("Cleaning up");
          setVisible(false);
          System.out.println("Finished");
          System.exit(0);
      }
      private void grabScreenShot() throws Exception
      {
          BufferedImage image = (BufferedImage)createImage(getContentPane().getSize().width,getContentPane().getSize().height);
          getContentPane().paint(image.getGraphics());
          try{
              ImageIO.write(image, "png", new File("C:\\Users\\"+TimeTable.user+"\\AppData\\TimeLineMacroProgram\\TimeLine.png"));
              System.out.println("Image was created");
          }
          catch (IOException e){
              System.out.println("Had trouble writing the image.");
              throw e;
          }
      }
  }
  </code></pre>