У меня есть кнопка, на которую я хочу, чтобы JFileChooser всплывал. Я попытался этоJFileChooser на кнопке Нажмите
JButton browse= new JButton("Browse");
add(browse);
browse.addActionListener(new ClassBrowse());
public class ClassBrowse implements ActionListener {
public void actionPerformed(ActionEvent e) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
// return the file path
} catch (Exception ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
}
else {
System.out.println("File access cancelled by user.");
}
}
}
Bhe выше дает ошибку The method showOpenDialog(Component) in the type JFileChooser is not applicable for the arguments (ClassName.ClassBrowse)
Кроме того, я хочу, чтобы вернуть полный путь к файлу. Как мне это сделать?
Заменить '' int returnVal = fileChooser.showOpenDialog (this); '' с '' int returnVal = fileChooser.showOpenDialog (frame); '', предполагая, что у вас где-то установлен JFrame, и что это только часть ваш код. – santafebound