javafx的导出文件选择路径

这个选择路径是当时学习别人的内容,由自己修改获得的。

分别有导入、导出、另存为按钮触发的内容


//导入按钮
Image image_in = new Image("file:pictures/in.png");//设置触发按钮的图片
in.setGraphic(new ImageView(image_in));
in.setOnAction((ActionEvent e) -> {
System.out.println("import");
Stage stage = new Stage();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("导入拓扑图");
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("XML", "*.xml"),
new FileChooser.ExtensionFilter("FXML", "*.fxml"),
new FileChooser.ExtensionFilter("Allfiles", "*.*"));


fileChooser.setInitialDirectory(new File("E://workspace//RouteSim//src//siminterface"));// System.getProperty("user.home")));
fileChooser.setInitialFileName("saveImgToList.xml");
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
try {
XmlSaveToFile.readTuoPuXml(drop_pane, file.getAbsolutePath());
initialize();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
;
System.out.println(file);
});
//导出按钮
Image img_out = new Image("file:pictures/out.png");
export.setGraphic(new ImageView(img_out));
export.setOnAction((ActionEvent e) -> {


System.out.println("export");

Stage stage = new Stage();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("导出拓扑图");


fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("XML", "*.xml"),
new FileChooser.ExtensionFilter("FXML", "*.fxml"),
new FileChooser.ExtensionFilter("Allfiles", "*.*"));


fileChooser.setInitialDirectory(new File("E://workspace//RouteSim//src//siminterface"));// System.getProperty("user.home")));
fileChooser.setInitialFileName("saveImgToList.xml");
File file = fileChooser.showSaveDialog(stage);
if (file != null) {
try {
XmlSaveToFile.saveTuoPuXml(file.getAbsolutePath());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
;
System.out.println(file);
});
//另存为按钮
Image img_save = new Image("file:pictures/saveas.png");
save.setGraphic(new ImageView(img_save));
save.setOnAction((ActionEvent e) -> {
FileChooser fileChooser = new FileChooser();  
      FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("file (*.xml)", "*.xml");  
      fileChooser.getExtensionFilters().add(extFilter);  
      Stage s = new Stage();  
      File file = fileChooser.showSaveDialog(s);  
      if(file==null){
      return;  
      }
      if (file != null) {
try {
XmlSaveToFile.saveTuoPuXml(file.getAbsolutePath());
Stage primaryStage = new Stage();
GridPane root = new GridPane();
Scene scene = new Scene(root,160,50);
Label message = new Label("        保存成功!");
message.setFont(Font.font("Timer New Roman",FontWeight.BOLD, FontPosture.ITALIC, 16));
Button ok = new Button("确定");
ok.setOnAction((ActionEvent e1) ->primaryStage.close());
root.add(message,3,0);
root.add(ok, 40, 2);
primaryStage.setTitle("提示");
primaryStage.getIcons().add(new Image("routesim/ok.jpg"));
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});

你可能感兴趣的:(javafx的导出文件选择路径)