This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Succeeding with AI and have Veljko Krunic on-line!
See this thread for details.
Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

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

 
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
 
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));
 
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!!!
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic