JavaFX使用SwingNode嵌入JFileChooser并获取所选文件 [英] JavaFX embed JFileChooser using SwingNode and get selected file
本文介绍了JavaFX使用SwingNode嵌入JFileChooser并获取所选文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已使用 SwingNode .该对话框显示并可用,但是我不确定如何从中获取所选文件.
I have integrated a JFileChooser into a JavaFX application using a SwingNode. The dialog displays and is usable, but I am unsure about how to get the selected file from it.
感谢您的帮助.
@FXML
public void openDialog(MouseEvent event) {
SwingNode swingNode = new SwingNode();
SwingUtilities.invokeLater(() -> {
swingNode.setContent(fileChooser);
});
BorderPane pane = new BorderPane();
pane.setCenter(swingNode);
Stage stage = new Stage();
Scene scene = new Scene(pane, 500, 500);
stage.setScene(scene);
stage.show();
}
推荐答案
出于好奇,我举了一个小例子来说明这一点.
Out of curiosity I made a small example to do this.
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.embed.swing.SwingNode;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
public class JunkFx extends Application {
public void openDialog(MouseEvent event) {
JFileChooser fileChooser = new JFileChooser("why");
SwingNode swingNode = new SwingNode();
SwingUtilities.invokeLater(() -> {
swingNode.setContent(fileChooser);
});
BorderPane pane = new BorderPane();
pane.setCenter(swingNode);
Stage stage = new Stage();
Scene scene = new Scene(pane, 500, 500);
stage.setScene(scene);
stage.show();
fileChooser.addActionListener(evt->{
System.out.println(evt.getActionCommand());
System.out.println(fileChooser.getSelectedFile());
Platform.runLater(stage::hide);
});
}
@Override
public void start(Stage stage) throws Exception {
Button b = new Button("click");
b.setOnMouseClicked(this::openDialog);
Scene scene = new Scene(new StackPane(b), 640, 480);
stage.setTitle("open a dialog");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args){
launch(args);
}
}
EDT上将有一个响应,然后您必须管理该响应的处理方式,检查是否已取消或接受等,然后在平台线程上触发操作.
There will be a response on the EDT, then you have to manage what to do with it, checking for cancelled or accepted etc. then fire an action onto the Platform thread.
这篇关于JavaFX使用SwingNode嵌入JFileChooser并获取所选文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文