We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,001 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Displaying image

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
1 Hour
Discussion Span
7 Months Ago
Last Updated
3
Views
fariajan.siddiqui
Newbie Poster
1 post since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0580 seconds using 2.66MB