1. 程式人生 > >Java檔案(夾)選擇器—JavaFX

Java檔案(夾)選擇器—JavaFX

在不久之前,我遇到一個問題,同學問我,記事本的開啟與儲存應該怎麼寫,當時我這樣想,應該可以根據File類獲得路徑的子檔案,用搜索演算法構造一個目錄樹,然後再做個圖形介面就行,沒什麼問題的話這樣做應該可以達到效果,但是今天我突然發現JavaFX裡面原本就有這個東西,不需要我們再重複造輪子了。

檔案選擇器——FileChooser

包:import javafx.stage.FileChooser;

     1、開啟檔案——File showOpenDialog(Window)

在window視窗開啟檔案選擇器,返回被選擇檔案的一個File物件,得到File我們也就可以對檔案進行處理。

       程式碼示例

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import java.io.File;
public class Test extends Application {
    public void start(Stage stage){
        Button btOpen=new Button("開啟檔案");
        Scene scene=new Scene(btOpen);
        FileChooser chooser=new FileChooser();
        chooser.setInitialDirectory(new File("C:\\Users\\Administrator\\Pictures"));   //設定初始路徑,預設為我的電腦
        chooser.setTitle("開啟圖片");                //設定視窗標題,預設為“開啟”
        chooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JPG", "*.jpg"),
                                             new FileChooser.ExtensionFilter("PNG", "*.png"));
        //篩選檔案擴充套件
        btOpen.setOnMouseClicked(e->{
            try{
                System.out.println(chooser.showOpenDialog(stage).getAbsolutePath()); //chooser.showOpenDialog(stage)得到File物件
            }catch (NullPointerException ex){}

        });
        stage.setScene(scene);
        stage.show();
    }
}

    多種型別:

chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("圖片", "*.jpg","*.png","*.gif")

    多個檔案:

List list=chooser.showOpenMultipleDialog(stage);

    使用

 

  2、儲存檔案 ——File showSaveDialog(Window)

 

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import java.io.File;
public class Test extends Application {
    public void start(Stage stage){
        Button btOpen=new Button("儲存圖片");
        Scene scene=new Scene(btOpen);
        FileChooser chooser=new FileChooser();
        chooser.setInitialDirectory(new File("C:\\Users\\Administrator\\Pictures"));   //設定初始路徑,預設為我的電腦
        chooser.setTitle("儲存圖片");                //設定視窗標題,預設為“開啟”
        chooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JPG", "*.jpg"),
                                             new FileChooser.ExtensionFilter("PNG", "*.png"));
        //篩選檔案擴充套件
        btOpen.setOnMouseClicked(e->{
            try{
                System.out.println(chooser.showSaveDialog(stage).getAbsolutePath()); //chooser.showOpenDialog(stage)得到File物件
            }catch (NullPointerException ex){}

        });
        stage.setScene(scene);
        stage.show();
    }
}

資料夾選擇器——DirectoryChooser

包:import javafx.stage.DirectoryChooser;

 用法同FileChooser差不多

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import java.io.File;
public class Test extends Application {
    public void start(Stage stage){
        Button btOpen=new Button("開啟資料夾");
        Scene scene=new Scene(btOpen);
        DirectoryChooser chooser=new DirectoryChooser();
        chooser.setInitialDirectory(new File("C:\\Users\\Administrator\\Pictures"));   //設定初始路徑,預設為我的電腦
        chooser.setTitle("開啟資料夾");                //設定視窗標題
        btOpen.setOnMouseClicked(e->{
            try{
                System.out.println(chooser.showDialog(stage)); //chooser.showDialog(stage)得到File物件
            }catch (NullPointerException ex){}

        });
        stage.setScene(scene);
        stage.show();
    }
}