JavaFX中是否有类似于returns一个文件的getSelectedFile的方法?
Is there a method similar to getSelectedFile in JavaFX that returns a File?
JavaFX中有没有类似getSelectedFile
的方法returns一个File
?我已经尝试过这段代码,现在试图完善注释掉的 //fileToSend[0]
@FXML
private void chooseButtonAction(ActionEvent event) throws IOException {
FileChooser JFileChooser = new FileChooser();
JFileChooser.setTitle("Choose a file to send");
Stage stage = (Stage) anchorPane.getScene().getWindow();
File file = JFileChooser.showOpenDialog(stage);
if (file != null) {
//fileToSend[0] = JFileChooser.getSelectedFile();
fileName.setText("File Name " + file.getName());
}
}
Swing 组件JFileChooser
offers getSelectedFile()
and getSelectedFiles()
, which returns an array "of selected files if the file chooser is set to allow multiple selection." In contrast, the JavaFX control FileChooser
returns a single File
from showOpenDialog()
, and it returns a List<File>
from showOpenMultipleDialog()
.
给定一个 FileChooser
实例,您可以像这样获取 selected
个文件的列表:
FileChooser fileChooser = new FileChooser();
List<File> selected = fileChooser.showOpenMultipleDialog(stage);
你可以这样获取第一个selected
文件:
System.out.println(selected.get(0));
您可以像这样遍历它们:
selected.forEach(file -> System.out.println(file.getName() + " selected"));
下面的完整示例是基于这个相关的 example:
import java.io.File;
import java.util.List;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
/** @see */
public class FileChooserDemo extends Application {
private static final int PADDING = 16;
@Override
public void start(Stage stage) {
stage.setTitle("FileChooserDemo");
FileChooser fileChooser = new FileChooser();
Label label = new Label("Select one or more files:");
TextArea textArea = new TextArea();
textArea.setPrefColumnCount(16);
textArea.setPrefRowCount(4);
Button singleButton = new Button("Select a File");
singleButton.setOnAction((ActionEvent t) -> {
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
textArea.clear();
textArea.appendText(file.getName());
}
});
Button multiButton = new Button("Select Files");
multiButton.setOnAction((ActionEvent t) -> {
List<File> selected = fileChooser.showOpenMultipleDialog(stage);
if (selected != null) {
textArea.clear();
selected.forEach(file -> textArea.appendText(file.getName() + "\n"));
}
});
VBox vBox = new VBox(PADDING, label, singleButton, multiButton, textArea);
vBox.setAlignment(Pos.CENTER);
vBox.setPadding(new Insets(PADDING));
Scene scene = new Scene(vBox);
stage.setScene(scene);
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}
JavaFX中有没有类似getSelectedFile
的方法returns一个File
?我已经尝试过这段代码,现在试图完善注释掉的 //fileToSend[0]
@FXML
private void chooseButtonAction(ActionEvent event) throws IOException {
FileChooser JFileChooser = new FileChooser();
JFileChooser.setTitle("Choose a file to send");
Stage stage = (Stage) anchorPane.getScene().getWindow();
File file = JFileChooser.showOpenDialog(stage);
if (file != null) {
//fileToSend[0] = JFileChooser.getSelectedFile();
fileName.setText("File Name " + file.getName());
}
}
Swing 组件JFileChooser
offers getSelectedFile()
and getSelectedFiles()
, which returns an array "of selected files if the file chooser is set to allow multiple selection." In contrast, the JavaFX control FileChooser
returns a single File
from showOpenDialog()
, and it returns a List<File>
from showOpenMultipleDialog()
.
给定一个 FileChooser
实例,您可以像这样获取 selected
个文件的列表:
FileChooser fileChooser = new FileChooser();
List<File> selected = fileChooser.showOpenMultipleDialog(stage);
你可以这样获取第一个selected
文件:
System.out.println(selected.get(0));
您可以像这样遍历它们:
selected.forEach(file -> System.out.println(file.getName() + " selected"));
下面的完整示例是基于这个相关的 example:
import java.io.File;
import java.util.List;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
/** @see */
public class FileChooserDemo extends Application {
private static final int PADDING = 16;
@Override
public void start(Stage stage) {
stage.setTitle("FileChooserDemo");
FileChooser fileChooser = new FileChooser();
Label label = new Label("Select one or more files:");
TextArea textArea = new TextArea();
textArea.setPrefColumnCount(16);
textArea.setPrefRowCount(4);
Button singleButton = new Button("Select a File");
singleButton.setOnAction((ActionEvent t) -> {
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
textArea.clear();
textArea.appendText(file.getName());
}
});
Button multiButton = new Button("Select Files");
multiButton.setOnAction((ActionEvent t) -> {
List<File> selected = fileChooser.showOpenMultipleDialog(stage);
if (selected != null) {
textArea.clear();
selected.forEach(file -> textArea.appendText(file.getName() + "\n"));
}
});
VBox vBox = new VBox(PADDING, label, singleButton, multiButton, textArea);
vBox.setAlignment(Pos.CENTER);
vBox.setPadding(new Insets(PADDING));
Scene scene = new Scene(vBox);
stage.setScene(scene);
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}