-1

I am working on a JFrame/panel that will contain a button. When the user clicks the button, I want an image (which will be stored in the computer hard disk beforehand) to open on the front screen.

button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        //here i want a code that will somehow open the image from a given directory
            }});

Any suggestions on how to go about this ? I have to tell where the image is stored and trigger a virtual 'double click' for the image to pop up on the front screen. Is that even possible using java to synchronize such computer functions?

razshan
  • 1,128
  • 12
  • 41
  • 59
  • this appears to be what you're after http://stackoverflow.com/questions/526037/java-how-to-open-user-system-preffered-editor-for-given-file – Karl Dec 02 '10 at 22:53

4 Answers4

3

I don't know a very short way, but I would use something like this (as qick hack to get an impression):

  try {
    // this is a new frame, where the picture should be shown
    final JFrame showPictureFrame = new JFrame("Title");
    // we will put the picture into this label
    JLabel pictureLabel = new JLabel();

    /* The following will read the image */
    // you should get your picture-path in another way. e.g. with a JFileChooser
    String path = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg";
    URL url = new File(path).toURI().toURL();
    BufferedImage img = ImageIO.read(url);
    /* until here */

    // add the image as ImageIcon to the label
    pictureLabel.setIcon(new ImageIcon(img));
    // add the label to the frame
    showPictureFrame.add(pictureLabel);
    // pack everything (does many stuff. e.g. resizes the frame to fit the image)
    showPictureFrame.pack();

    //this is how you should open a new Frame or Dialog, but only using showPictureFrame.setVisible(true); would also work.
    java.awt.EventQueue.invokeLater(new Runnable() {

      public void run() {
        showPictureFrame.setVisible(true);
      }
    });

  } catch (IOException ex) {
    System.err.println("Some IOException accured (did you set the right path?): ");
    System.err.println(ex.getMessage());
  }
Andreas L.
  • 142
  • 2
  • 9
0

Try this code

 try
{
  // the line that reads the image file
  BufferedImage image;


  // work with the image here ...
        image = ImageIO.read(new File("C://Users//Neo//Desktop//arduino.jpg"));


        jLabel1.setIcon(new ImageIcon(image));
} 
catch (IOException e)
{
  // log the exception
  // re-throw if desired
}
Neo
  • 1,359
  • 14
  • 34
0

I'm not sure but try this...

try
{

    JLabel picture=new JLabel();

    ImageIcon ic=new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("C:\\Users\\Desktop\\xyz.jpg")));

    picture.setIcon(ic);

}
catch(Exception)
{
}
ap91
  • 78
  • 1
  • 7
0

I think this will work ...

Code:

process = new ProcessBuilder("mspaint","yourFileName.jpeg").start();

This will open your image file with mspaint.....

and also use *Java Advanced Imaging (JAI)*

Ravi Parmar
  • 1,392
  • 5
  • 24
  • 46