Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 399
user841852 Avatar asked Jul 04 '26 00:07

user841852


1 Answers

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.

like image 153
Hovercraft Full Of Eels Avatar answered Jul 06 '26 16:07

Hovercraft Full Of Eels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!