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");

    if (chooser.showOpenDialog(this)== JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        ImageIcon icon = new ImageIcon(file.getName());
        imagePanel.add(new JLabel(icon));

Recommended Answers

All 2 Replies

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?

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()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.