tags:

views:

54

answers:

1

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

  1. user doesn't select any file
  2. user selects a non image file

In these cases I want to clear the JPanel and show an error message on a text area.

I tried to do this as below.But I am not sure if this is the way to do this properly.I would like your suggestions.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

class Display extends JFrame {

    private MyImagePanel canvas;
    private JButton okbutton;
    private JTextArea result;
    private JFileChooser filechooser;
    private static Insets insets = new Insets(0, 0, 0, 0);

    public Display(String name) {
        super(name);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());
        addWidgets();
        showGUI();
    }

    private void showGUI() {
        this.pack();
        this.setVisible(true);
    }

    private void addWidgets() {
        canvas = new MyImagePanel();
        okbutton = new JButton("OK");
        filechooser = new JFileChooser("Select imagefile");
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "Images", "jpg", "JPG", "GIF", "gif", "JPEG", "png", "PNG");
        filechooser.setFileFilter(filter);
        filechooser.setControlButtonsAreShown(false);
        result = new JTextArea(10, 10);
        addComponent(filechooser, 0, 0, 2, 4, GridBagConstraints.CENTER, GridBagConstraints.NONE);
        addComponent(canvas, 2, 0, 2, 2, GridBagConstraints.CENTER, GridBagConstraints.NONE);
        addComponent(result, 2, 2, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);
        addComponent(okbutton, 3, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);
    }

    public void addOKButtonListener(ActionListener okl) {
        okbutton.addActionListener(okl);
    }

    public void displayResult(String msg) {
        result.setText(msg);
    }

    public void clearResultField() {
        result.setText("");
    }

    public void displayImage(String imagefilename) {
        canvas.setImage(imagefilename);
    }

    public String getSelectedFile() {
        java.io.File selectedFile = filechooser.getSelectedFile();
        String filePathName = "";
        if (selectedFile == null) {
            result.setText("select a file");
        } else {
            filePathName = selectedFile.getPath();
        }
        return filePathName;
    }

    public void addComponent(Component component, int gridx, int gridy,
        int gridwidth, int gridheight, int anchor, int fill) {
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
            gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
        this.add(component, gbc);
    }
}//end class Display

class MyImagePanel extends JPanel {

    private BufferedImage bi;

    public MyImagePanel() {
        super();
        bi = null;
    }

    public void setImage(String imagefilename) {
        try {
            bi = ImageIO.read(new File(imagefilename));
            this.setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));
        } catch (Exception e) {
            bi = null;
        }
        this.revalidate();
        this.repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        // clear the background
        g.setColor(getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());
        if (bi != null) {
            // draw the image
            g.drawImage(bi, 0, 0, null);
        }
    }
}

class GUIController {

    private Display display;

    public GUIController(Display d) {
        display = d;
        display.addOKButtonListener(new OKButtonListener());
    }

    class OKButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            display.clearResultField();//make result field blank
            String fileselection = display.getSelectedFile();
            if (fileselection.length() > 0) {
                display.displayImage(fileselection);
            } else {
                display.displayResult("actionPerformed:no file selected");
                display.displayImage("");
            }
        }
    }
}

public class ImageDisplay {

    public static void main(String[] args) {
        Display d = new Display("image demo");
        GUIController ctrl = new GUIController(d);
    }
}
+1  A: 

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 http://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);
            }
        });
    }
}
trashgod
@trashgod: I tried the following..1.selected an image file->displays ok.. 2.selected a directory ->the previous image file is cleared from jpanel and an error msg is displayed on text area..This will only happen onlyif display.displayImage("") is called..I know this is a dirty way of doing it..Is there a better way of clearing the image? If I remove the above call ,then the setImage() of ImagePanel is never called when a directory is selected.
markjason72
@markjason72: I've elaborated above.
trashgod