0

hey iam writing this code in button's actionPerformed but it doesn't displaying any image . Can you identify what is problem in it???

JFileChooser chooser = new JFileChooser();
   FileNameExtensionFilter filter = new FileNameExtensionFilter("Images","jpeg", "jpg", "JPG", "JPEG", "gif","GIF", "png","PNG");

    chooser.setFileFilter(filter);
    if (chooser.showOpenDialog(this)== JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        ImageIcon icon = new ImageIcon(file.getName());
        imagePanel.add(new JLabel(icon));
        }
3
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by JamesCherrill
0

Can you make a small complete program that compiles, executes and shows the problem?

Does the code tell the container that the JLabel is added to that it needs to do a new layout of its contents?

0

file.getName() just returns the file name - any path info is lost. So unless the file happened to be in the current working directory it won't get found just from the name. You could try getting the full path & name to identify the file fully, eg file.getCanonicalPath() rather than just file.getName()

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.