我正在尝试在fxml文件中创建一个fileChooser.我的代码看起来像这样:
<HBox alignment="CENTER">
<Label text="Tower 1 Image" />
<TextField fx:id="tower1ImageField" />
<FileChooser fx:id ="tower1FileChooser" />
</HBox>
Run Code Online (Sandbox Code Playgroud)
控制器读起来像这样:
public class HudBuilderController{
@FXML TextField tower1ImageField;
@FXML FileChooser tower1FileChooser;
File towerFile;
@FXML TextField tower2ImageField;
@FXML FileChooser tower2FileChooser;
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到一个我不明白的错误:
Caused by: java.lang.IllegalArgumentException: Unable to coerce javafx.stage.FileChooser@5e85f35 to class javafx.scene.Node.
at com.sun.javafx.fxml.BeanAdapter.coerce(Unknown Source)
at javafx.fxml.FXMLLoader$Element.add(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(Unknown Source)
at javafx.fxml.FXMLLoader.processEndElement(Unknown Source)
... 26 more
Run Code Online (Sandbox Code Playgroud)
我已经尝试在控制器中实例化FileChooser,但我想我需要在fxml文件中添加更多内容.有帮助吗?谢谢!
该FileChooser犯规的延伸Node,因此,你不能在你使用它FXML.别忘了FXML只是用户界面的一种表示.无需将要在Controller中使用的所有组件添加到FXML.
您只需FileChooser在控制器中初始化a :
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showOpenDialog(primaryStage);
System.out.println(file);
Run Code Online (Sandbox Code Playgroud)
最后FileChooser是一个在屏幕上打开的对话框.不确定为什么要在FXML中使用它?只需在代码中使用它并使用您获得的文件路径.
| 归档时间: |
|
| 查看次数: |
10941 次 |
| 最近记录: |