• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HELP! me pls i can't open image on JLabel

 
duangna lim
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i try to choose image file from JFilechooser to open on a label ,but i still can't open any image T__T is there any worng in my code
this is the function that use to chooser file:
public void OpenImage(){

JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(false);
fc.addChoosableFileFilter(new ImageFilter());
fc.setFileView(new imageFileView());
fc.setAccessory(new imagePreview(fc));
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

int returnVal = fc.showOpenDialog(this);

if(returnVal == JFileChooser.CANCEL_OPTION)
{ file = null; }
else
{ file = fc.getSelectedFile();}

}/*end OpenImage*/

and i took the file value to the other function ,it's in the same class .

/********************************/
/*label for image*/
/********************************/
String ImgName = file.getPath();
ImgName = file.toString();
ImgName = ImgName.replace('\\','/');
Image image = Toolkit.getDefaultToolkit().getImage(ImgName);
ImageIcon Img = new ImageIcon(image);
JLabel photoLabel = new JLabel(Img);
photoLabel.setIcon(Img);

JPanel photoPanel = new JPanel();
photoPanel.add(photoLabel);
tell me pleasse what's worng with me
thanks for every help
 
Venkatesh Chitnis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this...
fc.setFileView(new ImageFileView());
fc.setAccessory(new ImagePreview(fc));
instead of
fc.setFileView(new imageFileView());
fc.setAccessory(new imagePreview(fc));
 
Fuqiang Zhao
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use the following code:
ImageIcon icon = new ImageIcon(file.toURL());
instead of your mass code:
String ImgName = file.getPath();
ImgName = file.toString();
ImgName = ImgName.replace('\\','/');
 
duangna lim
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your help i'll try that
very thank you!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic