Opening an image with JFileChooser

Ok, I’ll be honest here. I have NO IDEA what the deal is with urls and image input and why they never seem to work me. And when it does, it never lasts long.

Anyway, I need help in getting bufferedimage to load properly by selecting an image file with JFileChooser.

I have no working code. I have no code that even resembles working code. Please help a dying programmer ;_;

If I can recommend, do not use JFileChooser unless absolute necessary. It just does not work as should be, try compare it to the native filedialog.

Example code below opens a native FileOpen dialog and then we read it assuming its an image file.


import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
...cut cut...
public void btnSelectFileClicked(ActionEvent e) {
      FileDialog fd = new FileDialog(frame, "Select file", FileDialog.LOAD);
      fd.setVisible(true);
      String file = fd.getFile();
      if (file == null) 
         return;
      file = fd.getDirectory() + file; // "c:/sub/file.xyz"
      BufferedImage image = ImageIO.read(new File(file));
}

works. thx m8