user841852
user841852

Reputation: 187

File chooser in java

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.

Upvotes: 3

Views: 1356

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

Your problem is that you're making this call in a static method, main(...), and are trying to use FileChooserDemo.this (a reference to the enclosing class) inside of this static method. Well this won't work because there is no this in the static world. The solution is to do this in non-static code such as a non-static method or the class's constructor.

Upvotes: 3

Related Questions