I am trying to use file chooser in my code, I am getting an error "Not an enclosing class" in "int returnVal = fc.showOpenDialog(FileChooserDemo.this);". Below is my code. ANy guesses to solve it?
browse_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
File file = fc.getSelectedFile();
log.append("Opening: " + file.getAbsolutePath() + "." + "\n");
String ab=file.getAbsolutePath();
System.out.println(ab);
}});
I have made the actionlistener in main method.
Your problem is that you’re making this call in a static method,
main(...), and are trying to useFileChooserDemo.this(a reference to the enclosing class) inside of this static method. Well this won’t work because there is nothisin the static world. The solution is to do this in non-static code such as a non-static method or the class’s constructor.