[英]ShowopenDialog not working under actionlistener
So I have been trying to make a filechooser that opens a text file and then paste the contents in a JtextArea I have defined as textArea. 因此,我一直试图制作一个打开文本文件的文件选择器,然后将内容粘贴到我已定义为textArea的JtextArea中。 But I cannot get my showOpenDialog to not give an error while having the argument (this), and I researched and the answer was to fill in (null) and this does make the filechooser work but when I try to print the contents of it it also just returns null. 但是我不能让showOpenDialog在有参数(this)时不给出错误,我进行了研究,答案是填写(null),这确实使filechooser起作用,但是当我尝试打印它的内容时也只是返回null。 I am using the Eclipse program hence the auto filled code. 我正在使用Eclipse程序,因此使用自动填充的代码。 I am fairly new to Java and have no clue what is going wrong. 我对Java相当陌生,不知道出了什么问题。 Im really sorry if this is not the way to post things here. 如果这不是在此处发布内容的方式,我真的很抱歉。
JButton btnNewButton = new JButton("Bladeren");
btnNewButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println("Selected file: " + selectedFile.getAbsolutePath());
String content = readFile(selectedFile, StandardCharsets.UTF_8);
System.out.println(content);
textArea.setText(content);
}
}
private String readFile(File selectedFile, Charset utf8) {
// TODO Auto-generated method stub
return null;
}
}
);
You can look into the API that the parameter has to be from type Component
. 您可以查看API ,该参数必须来自Component
类型。 So what does this
mean in your example? 那么在您的示例中this
意味着什么? Which other class/interface does your class extends
from or implements
? 您的课程从哪个其他类/接口extends
或implements
?
You didn't paste the code of your readFile
method but ask why it returns null? 您没有粘贴readFile
方法的代码,但问为什么它返回null? This way we cannot help you so please post the code. 这样我们就无法为您提供帮助,因此请发布代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.