0

I am developing an image editing app, so want to display an image selected by JFileChooser, so what would be best approach so that it can display all formats jpg, png, gif etc. OpenButton is used for invocation of filechooser.

private void OpenActionPerformed(java.awt.event.ActionEvent evt) {
       int returnVal = fileChosser.showOpenDialog(this);
       if (returnVal == JFileChooser.APPROVE_OPTION) {
           File file = fileChosser.getSelectedFile();
           // What to do with the file
           // I want code for this part
           try {
             //code that might create an exception 
           } 
           catch (Exception e1) {
             e.printStackTrace();
           }
       }
}
Sanath
  • 4,774
  • 10
  • 51
  • 81
Tushar Maroo
  • 325
  • 1
  • 5
  • 24

1 Answers1

1

The easiest way is probably to create an ImageIcon from the URL of the file (or from the content of the file as bytes, or from the file name), and to wrap this ImageIcon into a JLabel:

iconLabel.setIcon(new ImageIcon(file.toURI().toURL()));

But if your app is supposed to edit the image, then you'll have to learn how to manipulate java.awt.Image instances, and the easiest way won't be sufficient.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • i tried above snippet it worked but to edit suggest me all methods that can be used..and is there any method which can load image as 2-d array?? – Tushar Maroo Jun 02 '12 at 21:12
  • *"can load image as 2-d array??"* Check the JavaDocs for `BufferedImage`. Also, fix that stuck '?' key. – Andrew Thompson Jun 02 '12 at 21:18
  • 1
    You won't learn how to build an image edition application by starting from scratch and asking questions on StackOverflow. Read the documentation of java.awt.Image and of its subclasses, and at least read the tutorial about Java 2D graphics: http://docs.oracle.com/javase/tutorial/2d/index.html. Reading documentation is a requirement for any decent developer. – JB Nizet Jun 02 '12 at 21:22