JFileChooser to set a JLabel Icon

Well the subject pretty much says it all. I want to use a JFileChooser to set the Icon for a JLabel. Here is what I have done - it makes sense because the getSelectedFile returns the file name and path, and the setIcon(new ImageIcon()) requires the string path and file to create the icon. But it doesnt compile.

JFileChooser file =new JFileChooser();

file.showOpenDialog(this);

label1.setIcon(new ImageIcon(file.getSelectedFile()));

Its probably giving the error because the user could select an exe file and it wouldn't work. If I figure out how to use filefilters would that help?

Any advice?

[733 byte] By [SidewinderXa] at [2007-11-27 10:18:47]
# 1

> But it doesnt compile.

> Its probably giving the error because the user could select an exe file and it wouldn't work.

That has nothing to do with a compile error.

Since we don't know what the compile error is or what you code looks like we won't be able to help.

If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",

see http://homepage1.nifty.com/algafield/sscce.html,

that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

> If I figure out how to use filefilters would that help?

The API has an example and it also has a link to the Swing tutorial on "How to Use File Chooser" which has another example.

camickra at 2007-7-28 16:53:40 > top of Java-index,Desktop,Core GUI APIs...
# 2

The problem was the getSelectedFile() returns a File not a String - getSelectedFile().getPath() returns the String path. :)

SidewinderXa at 2007-7-28 16:53:40 > top of Java-index,Desktop,Core GUI APIs...