I am learning Swing and wrote an app that lets user select an image file and displays it on a JPanel
. It works, but I want to handle situations when
The several drawImage()
methods in java.awt.Graphics do "nothing if img
is null
." As a result, setting the image to null
is sufficient. You're clearing the background explicitly, but super.paintComponent(g)
is an alternative that clears the panel to the background color.
Addendum: You may also want to study the examples found in the articles How to Use File Choosers and Working with Images.
Addendum: I used a different layout and added the image to a JScrollPane
. I also had setImage()
return a result to let the Display
know what happened.
Addendum: This newer, simpler revision extends JFileChooser
to handle approve and cancel directly.
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
/** @see https://stackoverflow.com/questions/4053090 */
public class ImageDisplay extends JFrame {
private static final String title = "Select a file";
private MyImagePanel imagePanel = new MyImagePanel();
private JLabel result = new JLabel(title, JLabel.CENTER);
private MyChooser fileChooser = new MyChooser();
public ImageDisplay(String name) {
super(name);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWidgets();
this.pack();
this.setVisible(true);
}
private void addWidgets() {
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Images", "jpg", "JPG", "GIF", "gif", "JPEG", "png", "PNG");
fileChooser.setFileFilter(filter);
this.add(fileChooser, BorderLayout.WEST);
this.add(new JScrollPane(imagePanel), BorderLayout.CENTER);
this.add(result, BorderLayout.SOUTH);
}
class MyChooser extends JFileChooser {
@Override
public void approveSelection() {
File f = fileChooser.getSelectedFile();
if (imagePanel.setImage(f)) {
result.setText(f.getName());
} else {
result.setText(title);
}
}
@Override
public void cancelSelection() {
imagePanel.setImage(null);
result.setText(title);
}
}
class MyImagePanel extends JPanel {
private BufferedImage bi;
public MyImagePanel() {
this.setPreferredSize(new Dimension(500, 700));
}
/** Return true if read() succeeded. */
public boolean setImage(File f) {
try {
bi = ImageIO.read(f);
} catch (Exception e) {
bi = null;
}
if (bi != null) {
setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));
}
this.revalidate();
this.repaint();
return bi != null;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bi, 0, 0, null);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ImageDisplay("Image Demo").setVisible(true);
}
});
}
}