codestory

Die Anleitung zu JavaFX FileChooser und DirectoryChooser

  1. JavaFX FileChooser, DirectoryChooser
  2. Das Beispiel über FileChooser
  3. Das Beispiel über DirectoryChooser
  4. für FileChooser, DirectoryChooser konfigurieren
  5. Den Filter FileChooser einstellen
  6. Zum Beispiel: Save File

1. JavaFX FileChooser, DirectoryChooser

FileChooser ermöglicht den Benutzer, ein oder viele File zu wählen. Gleichartig ermöglicht DirectoryChooser den Benutzer, ein oder viele Folder zu wählen
Unten ist die Image von FileChooser/DirectoryChooser wenn sie in den unterschiedlichen Betriebssystem läuft.
Mac OS
Windows
Linux

2. Das Beispiel über FileChooser

Beispiel, Wenn der Benutzer die Button klickt, erscheint ein FileChooser damit der Benutzer die File wählen und öffnet sie
OpenFileChooserDemo.java
package org.o7planning.javafx.filechooser;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class OpenFileChooserDemo extends Application {

    private Desktop desktop = Desktop.getDesktop();

    @Override
    public void start(Stage primaryStage) throws Exception {

        final FileChooser fileChooser = new FileChooser();

        TextArea textArea = new TextArea();
        textArea.setMinHeight(70);

        Button button1 = new Button("Select One File and Open");

        Button buttonM = new Button("Select Multi Files");

        button1.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                textArea.clear();
                File file = fileChooser.showOpenDialog(primaryStage);
                if (file != null) {
                    openFile(file);
                    List<File> files = Arrays.asList(file);
                    printLog(textArea, files);
                }
            }
        });

        buttonM.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                textArea.clear();
                List<File> files = fileChooser.showOpenMultipleDialog(primaryStage);

                printLog(textArea, files);
            }
        });

        VBox root = new VBox();
        root.setPadding(new Insets(10));
        root.setSpacing(5);

        root.getChildren().addAll(textArea, button1, buttonM);

        Scene scene = new Scene(root, 400, 200);

        primaryStage.setTitle("JavaFX FileChooser (o7planning.org)");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void printLog(TextArea textArea, List<File> files) {
        if (files == null || files.isEmpty()) {
            return;
        }
        for (File file : files) {
            textArea.appendText(file.getAbsolutePath() + "\n");
        }
    }

    private void openFile(File file) {
        try {
            this.desktop.open(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

}

3. Das Beispiel über DirectoryChooser

Beispiel, DirectoryChooser ist benutzt um ein Ordner zu wählen
DirectoryChooserDemo.java
package org.o7planning.javafx.filechooser;

import java.io.File;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

public class DirectoryChooserDemo extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        final DirectoryChooser directoryChooser = new DirectoryChooser();
        configuringDirectoryChooser(directoryChooser);

        TextArea textArea = new TextArea();
        textArea.setMinHeight(70);

        Button button = new Button("Open DirectoryChooser and select a directory");

        button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                File dir = directoryChooser.showDialog(primaryStage);
                if (dir != null) {
                    textArea.setText(dir.getAbsolutePath());
                } else {
                    textArea.setText(null);
                }
            }
        });

        VBox root = new VBox();
        root.setPadding(new Insets(10));
        root.setSpacing(5);

        root.getChildren().addAll(textArea, button);

        Scene scene = new Scene(root, 400, 200);

        primaryStage.setTitle("JavaFX DirectoryChooser (o7planning.org)");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void configuringDirectoryChooser(DirectoryChooser directoryChooser) {
        // Set title for DirectoryChooser
        directoryChooser.setTitle("Select Some Directories");

        // Set Initial Directory
        directoryChooser.setInitialDirectory(new File(System.getProperty("user.home")));
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

}

4. für FileChooser, DirectoryChooser konfigurieren

Sie können die Titel für FileChooser/DirectoryChooser und den Ordner als Defaultsordner setzen wenn der Benutzer FileChooser/DirectoryChooser. öffnet
// Set title for FileChooser
fileChooser.setTitle("Select Some Files");

// Set Initial Directory
fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
ConfiguringFileChooserDemo.java
package org.o7planning.javafx.filechooser;

import java.io.File;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class ConfiguringFileChooserDemo extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        final FileChooser fileChooser = new FileChooser();
        configuringFileChooser(fileChooser);

        TextArea textArea = new TextArea();
        textArea.setMinHeight(70);

        Button button = new Button("Open File Chooser and select some files");

        button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                fileChooser.showOpenDialog(primaryStage);
            }
        });

        VBox root = new VBox();
        root.setPadding(new Insets(10));
        root.setSpacing(5);

        root.getChildren().addAll(button);

        Scene scene = new Scene(root, 400, 200);

        primaryStage.setTitle("JavaFX FileChooser (o7planning.org)");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void configuringFileChooser(FileChooser fileChooser) {
        // Set title for FileChooser
        fileChooser.setTitle("Select Some Files");

        // Set Initial Directory
        fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

}

5. Den Filter FileChooser einstellen

Zum nächsten können Sie die FileChooser in die Filter setzen zu entscheiden, welche Fileart in FileChooser angezeigt werden. Z.B: Erstellung von 3 Filter: ALL Filter zeigt alle Fileart, PNG Filter zeigt die PNG - Image, und JPG Filter zeigt die JPG Image.
// Add Extension Filters

fileChooser.getExtensionFilters().addAll(//
       new FileChooser.ExtensionFilter("All Files", "*.*"), 
       new FileChooser.ExtensionFilter("JPG", "*.jpg"), 
       new FileChooser.ExtensionFilter("PNG", "*.png"));
The filters:
All Filter : Anzeige von alle Filearten und auch Folder.
PNG Filter zeigt nur PNG Image und Folder
Beispiel
FilterFileChooserDemo.java
package org.o7planning.javafx.filechooser;

import java.io.File;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class FilterFileChooserDemo extends Application {

  @Override
  public void start(Stage primaryStage) throws Exception {

      final FileChooser fileChooser = new FileChooser();
      configuringFileChooser(fileChooser);

      TextArea textArea = new TextArea();
      textArea.setMinHeight(70);

      Button button = new Button("Select some Pictures");

      button.setOnAction(new EventHandler<ActionEvent>() {

          @Override
          public void handle(ActionEvent event) {
              fileChooser.showOpenDialog(primaryStage);
          }
      });

      VBox root = new VBox();
      root.setPadding(new Insets(10));
      root.setSpacing(5);

      root.getChildren().addAll(button);

      Scene scene = new Scene(root, 400, 200);

      primaryStage.setTitle("JavaFX FileChooser (o7planning.org)");
      primaryStage.setScene(scene);
      primaryStage.show();
  }

  private void configuringFileChooser(FileChooser fileChooser) {
      // Set title for FileChooser
      fileChooser.setTitle("Select Pictures");

      // Set Initial Directory
      fileChooser.setInitialDirectory(new File("C:/Users/tran/Pictures"));

      // Add Extension Filters
      fileChooser.getExtensionFilters().addAll(//
              new FileChooser.ExtensionFilter("All Files", "*.*"), //
              new FileChooser.ExtensionFilter("JPG", "*.jpg"), //
              new FileChooser.ExtensionFilter("PNG", "*.png"));
  }

  public static void main(String[] args) {
      Application.launch(args);
  }

}

6. Zum Beispiel: Save File

  • TODO

Die Anleitungen JavaFX

Show More