-2

Is there a method similar to getSelectedFile in JavaFX that returns a File? I have tried this code now trying to perfect the commented out //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());
    }

}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I don’t understand why your code mixes JavaFX and Swing. – jewelsea Mar 05 '22 at 10:52
  • 1
    Fascinating! Your code is pure JavaFX yet you declare a variable whose name is that of a _Swing_ class. I am referring to `JFileChooser`. Your commented line of code will not compile since class `javafx.stage.FileChooser` does not have a `getSelectedFile` method. In any case, you already have the selected file in this line of your code: `File file = JFileChooser.showOpenDialog(stage);`. Also, if `fileToSend` is a variable, I could not find its declaration in the code in your question. Did I miss something? – Abra Mar 05 '22 at 12:22
  • Well sorry for the naming conventions, I missed that part actually :). I am making the project in fx but couldn't find a similar fx method so went for the swing to show the method which I am looking for. And the code compiled it just opens a swing dialogbox in a fx program . – Parvez Ahammed Mar 05 '22 at 20:46

1 Answers1

1

The Swing component 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().

Given a FileChooser instance, you can obtain the list of selected files like this:

FileChooser fileChooser = new FileChooser();
List<File> selected = fileChooser.showOpenMultipleDialog(stage);

You can get the first selected file like this:

System.out.println(selected.get(0));

You can iterate through them like this:

selected.forEach(file -> System.out.println(file.getName() + " selected"));

The complete example below is based on this related example:

image

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 https://stackoverflow.com/a/71362723/230513 */
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);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045