Re: Display image selected from JFileChooser
Oops! I forgot the third class I wrote to handle the getting of the
file.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
public class myevent implements ActionListener{
BufferedImage image;
public void actionPerformed (ActionEvent e){
JFileChooser fileChooser = new JFileChooser();
int returnVal = fileChooser.showOpenDialog(fileChooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
System.out.println("Image selected: " + file.getPath());
System.out.println("Image about to be loaded to buffer");
try {
System.out.println("Image loaded to buffer");
image = ImageIO.read(fileChooser.getSelectedFile());
System.out.println("Image stored as a buffered image");
}
catch (IOException ex) {
System.out.println("problem accessing file
"+file.getAbsolutePath());
}
}
else {
System.out.println("File access cancelled by user.");
}
}
}