troubles with displaying image in window

I need to choose image with file open dialog and then show it in window. But When I choose image it is not shown in the window.

I've created class which create window with jmenubar and 1 jmenuitem. When I click on menuitem JfileChooser appears and then I choose some file. But then happens nothing. I think the problem is in actionListener for JFileChooser(ImageFilter is a filter from docs java)

public Frame(){
    //create bars and window
    mainframe = new JFrame("Window");
    mainframe.setVisible(true);
    mainframe.setSize(300, 300);
    menubar = new JMenuBar();
    mainer = new JMenu("Menu");
    menubar.add(mainer);
    //create items
    item = new JMenuItem("Open",KeyEvent.VK_T);
    item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
    item.getAccessibleContext().setAccessibleDescription("open image");
    //action listener
    item.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //open file dialog
                    choser = new JFileChooser();
                    choser.addChoosableFileFilter(new ImageFilter());
                    final int returnval = choser.showOpenDialog(menubar);
                    //action listener for JFileChooser
                    choser.addActionListener(
                            new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    if (returnval == JFileChooser.APPROVE_OPTION){
                                        fc = choser.getSelectedFile();
                                        try{
                                            Panel panel = new ShowImage(fc.getName());
                                            mainframe.getContentPane().add(panel);
                                        }catch(Exception exc){};
                                    }
                                }
                            }
                    );
                }
            }
    );
    mainer.add(item);
    mainframe.setJMenuBar(menubar);
}

ShowImage class

class ShowImage extends Panel{
    BufferedImage image;
    public ShowImage(String imagename) throws IOException {
        File input = new File(imagename);
        image = ImageIO.read(input);
    }
    public void paint(Graphics g){
        g.drawImage(image,0,0,image.getWidth(),image.getHeight(),null);
    }
}

P.S another problem is that it shows nothing until I change size of the window.

Upvotes: 2

Views: 115

Answers (2)

John Snow
John Snow

Reputation: 5344

Im pretty sure this line will cause problems:

Panel panel = new ShowImage(fc.getName()); 

getName() will return the name of the file. So for example if you choose a image with JFileChooser named image.jpg, getName will return "image.jpg". This will make the image only show if the file you select is stored in the root folder of your project. I would change getName() to getAbsoultePath() which will return the full patch (e.i c:\desktop\image.jpg) which is most likley what you want.

Also as Max points out, you should override paintComponent rather then paint:

protected void paintComponent(Graphics g){     
   g.drawImage(image,0,0,image.getWidth(),image.getHeight(),null); 
}

Upvotes: 2

tenorsax
tenorsax

Reputation: 21233

Extend JPanel instead of Panel, and override paintComponent method, ie:

class ShowImage extends JPanel{
    public void paintComponent(Graphics g){
        ...
    }
}

Also, there is no need to addActionListener on JFileChooser, just check the return value and act accordingly, ie:

final int returnval = choser.showOpenDialog(menubar);
if (returnval == JFileChooser.APPROVE_OPTION){
    ...
}

Upvotes: 2

Related Questions