slimeの日記

プログラミングの備忘録的なブログです。

 Javafxファイル・チューザの作成と使用方法

 

JavaFXのファイルチューザについて書かれているサイトがあまり見当たらなかったので、作成及び使用方法についてまとめます。

javafx scene builderを使用しています。

 

 

  • ファイルチューザの作成
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open File");
fileChooser.showOpenDialog(null);

で作成できます。

fileChooser.showOpenDialog(stage);でオーナーを設定します。

nullでも大丈夫です。

 

  • Buttonで開く
@FXML
	protected void fileaction(ActionEvent a) {
		FileChooser fileChooser = new FileChooser();
		fileChooser.setTitle("Open File");
		File file = fileChooser.showOpenDialog(null);
		}

 FXMLは省略します。

 

  • ファイルチューザの拡張子フィルタの設定
fileChooser.getExtensionFilters().addAll(
        new FileChooser.ExtensionFilter("All", "*.*"),
        new FileChooser.ExtensionFilter("PNG", "*.png")
            );

 ("タイトル", "名前.拡張子")と書きます。全てを選択する場合は*です。

 

fileChooser.setInitialDirectory(
	   new File(System.getProperty("user.home"))
	        ); 

 この場合ピクチャやビデオなどがあるuserホームを開きます。

 

  • 画像ファイルを開いてwindowに表示する
File file = fileChooser.showOpenDialog(null);

 ファイルを開いて表示するには、fileChooser.showOpenDialog(null);を上記のように変更します。

次に、ファイルのパスを取得します。

String url = "file:///"+file.getPath();

 最初に file:/// を付けないとエラーになってしまうので注意しましょう。

最後に、Imageviewとして表示します。

@FXML ImageView image1;
    .......
Image image2 = new Image(url);
image1.setImage(image2);

 

 

 Sample.FXML


    <!--?xml version="1.0" encoding="UTF-8"?-->

<!--?import javafx.scene.control.*?-->
<!--?import javafx.scene.image.*?-->
<!--?import java.lang.*?-->
<!--?import javafx.scene.layout.*?-->
<!--?import javafx.scene.layout.BorderPane?-->

<borderpane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController"><center>
      <pane prefheight="400.0" prefwidth="400.0" borderpane="" alignment="CENTER">
         <children>
            <imageview fx:id="image1" fitheight="186.0" fitwidth="200.0" layoutx="100.0" layouty="118.0" pickonbounds="true" preserveratio="true"></imageview>
            <button##/button>
         </children>
      </pane>
   </center>
</borderpane>

 Main.java

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
	@Override
	public void start(Stage primaryStage) {
		try {
			BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
			Scene scene = new Scene(root,400,400);
			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			primaryStage.setScene(scene);
			primaryStage.show();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		launch(args);
	}
}

 

SampleController.java

import java.io.File;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.FileChooser;

public class SampleController {
	
	@FXML Button file1;
	@FXML ImageView image1;
	
	
	@FXML
	protected void fileaction(ActionEvent a) {
		FileChooser fileChooser = new FileChooser();
		fileChooser.setTitle("Open File");
		fileChooser.setInitialDirectory(
	            new File(System.getProperty("user.home"))
	        ); 
		File file = fileChooser.showOpenDialog(null);
		
		String url = "file:///"+file.getPath();
	    Image image2 = new Image(url);
	    image1.setImage(image2);
	    System.out.println(url);
	}
	
}