1
我有一個帶有文本字段的FXML控制器類,我希望通過FileChooser來選擇文件的各種文件屬性。如何從JFileChooser中獲取選定文件的屬性
控制器看起來像:
@FXML
TextField documentName;
File file;
public void attachNewDocFileChooser() {
file = new MyFileChooser().chooser();
if (file != null) {
documentName.setText(file.getName());
} else {
documentName.setText("No file selected");
}
}
的文件選擇在不同的類MyFileChooser
創建:
@FXML
public File chooser() {
File file = null;
final JFileChooser fileDialog = new JFileChooser();
int returnVal = fileDialog.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fileDialog.getSelectedFile();
}
return file;
}
我無法填充選擇的文件名的文本框documentName
。
我將非常感謝任何幫助,使這項工作。謝謝大家。
更新: 我收到一個java.lang.NullPointerException
。
我也忘了提及chooser()
是鏈接到一個標籤,所以onMouseClicked="#chooser"
。
爲什麼不使用JavaFX的[文件選擇](http://docs.oracle.com/javafx/2/api/javafx/stage/FileChooser.html),而不是擺動的JFileChooser? – 2014-08-29 09:55:01
Hi @JoopEggen Eggen。我結束了。唯一的區別是我將使用[FileChooser](http://docs.oracle.com/javafx/2/api/javafx/stage/FileChooser.html),但仍然是同樣的問題。我真的不介意我將使用什麼,FileChooser或swing JFileChooser,我只想從控制器獲取'documentName.setText(file.getName());'從另一個類中檢索FileChooser。 – 2014-08-29 10:19:08