Reputation: 541
What I want is to display an image in a label after the user chooses it from a file chooser , I followed a lot of tutorials , one of them was this https://netbeans.org/kb/docs/java/gui-image-display.html it worked when I hard coded the image name , but what I really need is to display what the user chooses from the file chooser . I have tried this int retV; File file=null;
if (fc== null)
fc=new JFileChooser();
fc.addChoosableFileFilter(new ImageFilter());
fc.setAcceptAllFileFilterUsed(false);
retV =fc.showOpenDialog(GUI.this);
if (retV == JFileChooser.APPROVE_OPTION)
file = fc.getSelectedFile();
infoText.append("-The image : "+file.getAbsolutePath()+"\nwill be conveted to PNG format becuase it's a lossless\n compression.\n");
ImageIcon icon= new ImageIcon(file.getAbsolutePath());
img1=new JLabel(icon);
but it didn't work .
Any help is really appreciated
Upvotes: 1
Views: 4244
Reputation: 4808
From the information you have given...It's unclear what's the fault was...But to avoid those problems we get by paths...and absolute paths...use the following technique instead.
You are any ways getting the File
object. So, use that object to read the file and create a BufferedImage
which is a subclass of Image
and pass it to the constructor. Don't forget to check for null
when using objects.....
Use instead:
File file=fc.getSelectedFile();
BufferedImage bImage= ImageIO.read(file);
ImageIcon icon= new ImageIcon(img);
This is one of the solutions.... I hope this helps....
Upvotes: 1
Reputation: 917
Another option is converting the selected File
to an URL
:
selectedFile.toURI().toURL()
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class TrivialImageViewer implements Runnable {
private static final Font DEMO_FONT = new Font("Arial", Font.BOLD, 20);
private final JFrame frame;
private final JFileChooser fileChooser;
private final JLabel iconLabel;
private final class SelectImageAction extends AbstractAction {
private static final long serialVersionUID = 1L;
private SelectImageAction(final String name) {
super(name);
}
@Override
public void actionPerformed(final ActionEvent e) {
final int option = fileChooser.showOpenDialog(frame);
if (option != JFileChooser.APPROVE_OPTION) {
return;
}
final File selectedFile = fileChooser.getSelectedFile();
URL url;
try {
url = selectedFile.toURI().toURL();
} catch (final MalformedURLException e1) {
throw new RuntimeException(e1);
}
final ImageIcon icon= new ImageIcon(url);
iconLabel.setText("");
iconLabel.setIcon(icon);
}
}
public TrivialImageViewer() {
frame = new JFrame();
frame.setTitle("Trivial Swing ImageViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
iconLabel = new JLabel("Please select an image.");
iconLabel.setFont(DEMO_FONT);
iconLabel.setHorizontalAlignment(SwingConstants.CENTER);
fileChooser = new JFileChooser();
frame.add(iconLabel, BorderLayout.CENTER);
frame.add(new JButton(new SelectImageAction("Select Image...")), BorderLayout.SOUTH);
}
@Override
public void run() {
frame.setBounds(16, 16, 640, 480);
frame.setVisible(true);
}
public static void main(final String[] args) throws Exception {
// Use EventQueue.invokeLater in serious apps...
final TrivialImageViewer application = new TrivialImageViewer();
application.run();
}
}
Don't forget to implement proper resource management if you use this outside of the class room and: Have fun!
Upvotes: 2
Reputation: 967
Try this:
ImageIcon ii=new ImageIcon("filename");
JLabel label=new JLabel();
label.setIcon(ii);
Upvotes: 1