title: PrintScreen image capturing
body: <p>I need to write a program that I will create a runnable jar and distribute. The functions should be like below:</p>
  
  <p>when double click the jar, it will open a window.
  it will ask the path where to save the image files.
  it will then ask whether to add any prefix / suffix / both on every image along with timestamp for unique name.
  it will also ask what image format to use.
  the app can be minimized and closed
  it will take a full screenshot whenever PrintScreen is pressed and save.
  Please provide a programme that is complete. I have gathered pieces but could not put them in one. Here is my code :-</p>
  
  <pre><code>import java.awt.*;
  import java.awt.datatransfer.DataFlavor;
  import java.awt.datatransfer.Transferable;
  import java.awt.event.*;
  import java.awt.image.RenderedImage;
  import java.io.File;
  import java.util.Date;
  
  import javax.imageio.ImageIO;
  import javax.swing.*;
  
  public class MainClass
  {
  static String location = "";
  static String prefix = "";
  static String format = "";
  static Date timestamp = new Date();
  
  public static void main(String args[])
  {
      try 
      {
          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  JFrame f = new JFrame("Text Field Examples");
  f.getContentPane().setLayout(new FlowLayout());
  
  final JTextField textField1 = new JTextField("Enter Location To Save Image Files");
  textField1.addMouseListener(new MouseAdapter(){
      @Override
      public void mouseClicked(MouseEvent e){
          textField1.setText("");
      }
  });
  textField1.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
          location = textField1.getText();
          System.out.println(location);
      }
  });
  f.getContentPane().add(textField1);
  
  final JTextField textField2 = new JTextField("Enter Prefix or Leave Empty");
  textField2.addMouseListener(new MouseAdapter(){
      @Override
      public void mouseClicked(MouseEvent e){
          textField2.setText("");
      }
  });
  textField2.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
          prefix = textField2.getText();
          System.out.println(prefix);
      }
  });
  f.getContentPane().add(textField2);
  
  String  jlistData[] =
      {
          "GIF",
          "PNG",
          "JPG"
      };
  final JComboBox  jlist = new JComboBox&lt;String&gt;( jlistData );
  jlist.addActionListener(new ActionListener() {
  
      @Override
      public void actionPerformed(ActionEvent e) {
          format = jlist.getSelectedItem().toString();
          System.out.println(format);
  
      }
  });
  f.getContentPane().add(jlist);
  
  f.pack();
  f.setVisible(true);
  } 
  catch (Exception evt) 
  {
      evt.printStackTrace();
  }
  
  
  
  try
  {
      Robot robot = new Robot();
  
      robot.keyPress(KeyEvent.VK_PRINTSCREEN);
      robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
  
  
      Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
      RenderedImage image = (RenderedImage) t.getTransferData(DataFlavor.imageFlavor);
  
      ImageIO.write(image, format, new File(new String(location+prefix+image+timestamp)));
  }
  catch(Exception e)
  {
  
  }   
  
  }
  }
  </code></pre>
  
  <p>The first try catch block can open a window, take image format, prefix and storage location. The second try catch block alone can take screen shot when run not when printscreen key is pressed but with the first try catch it does not print anything. So, what to do to take the screenshot when printscreen key is pressed ?</p>