Re: Display image selected from JFileChooser
Reply to Lew:
Many thanks for your comments on my code. Yes my statement "it doesn't
work" was vague and not informative, apologies. As you can tell I am a
Java novice and could not diagnose the faults myself. From your
comments it is clear that my code contained many problems. I will read
more about EDT and logger (I have left my println statements in for
the time being as I am more concerned with getting the images to
display at the moment).
reply to John B. Matthews:
Many thanks for your comments also. I have read and digested your
code, most of which I can follow, but some is still too advanced for
me. I have read the Action page you posted the link to and have tried
to implement an action in my code.
Heavily influenced by John B. Matthews' code, I have rewritten much of
my code, however it still does not display an image. I cannot identify
the source of the problem, but based on the println statements (which
I will remove in place of a logger) I think that myAction (the action
associated with the button) is not being returned.
I have attached my new code. I would be most grateful if someone could
identify the problem and indicate how I could resolve it.
Many thanks,
Jimmy
import java.awt.event.ActionEvent;
class MyAction extends AbstractAction{
BufferedImage image;
public MyAction(){
super("Open");
}
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 painted");
}
catch (IOException ex) {
System.out.println("problem accessing
file"+file.getAbsolutePath());
}
}
else {
System.out.println("File access cancelled by user.");
}
}
}
import javax.imageio.ImageIO;
class MyImage extends JPanel{
private final Window parent;
BufferedImage img;
MyAction myAction = new MyAction();
public MyImage(JFrame parent){
this.parent = parent;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
public Action MyAction(){
return myAction;
}
}
import java.awt.event.ActionEvent;
class MyAction extends AbstractAction{
BufferedImage image;
public MyAction(){
super("Open");
}
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 painted");
}
catch (IOException ex) {
System.out.println("problem accessing
file"+file.getAbsolutePath());
}
}
else {
System.out.println("File access cancelled by user.");
}
}
}