Java Swing – JFileChooserの例
`JFileChooser`は、ユーザにファイルまたはファイル保存場所を選択させるための迅速かつ簡単な方法です。以下に、このクラスの使い方の簡単な例を示します。
`JFileChooser`には6つのコンストラクタがあります:
ディレクトリ
**
JFileChooser(String)
– 指定されたパスを使用する
-
JFileChooser(File)
– 与えられたFileをパスとして使います。 -
JFileChooser(FileSystemView)
– 与えられたFileSystemViewを使用します。 -
JFileChooser(String、FileSystemView)
– 指定されたパスと
FileSystemView
**
JFileChooser(File、FileSystemView)
– 与えられた電流を使用します。
ディレクトリと
FileSystemView
`JFileChooser`コンストラクタを呼び出す様々な方法
….//JFileChooser jfc;//String path = “C:\\Users\\Public”;//File file = new File(“C:\\Users\\Public”);//FileSystemView fsv = FileSystemView.getFileSystemView();
作者の個人的な好みは、 `FileSystemView`を考慮に入れることです。以下の例では、 `FileSystemView.getFileSystemView()`を使って、 `getHomeDirectory()`を通してホームディレクトリを指しています。そのプロセスはFile型になります。つまり、 `FileSystemView`を考慮しながら、` JFileChooser(File) `コンストラクタを使用しています。 === 1. show ** Dialog() - __ファイルを開く/保存する__ `JFileChooser`を使ってファイルを開くための絶対パスを取得する方法や、ファイルを保存したい場所を取得する方法の例: FileChooser1.java
package com.techfou.jfileChooser;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
public class FileChooser1 {
public static void main(String[]args) {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showOpenDialog(null); //int returnValue = jfc.showSaveDialog(null);
if (returnValue == JFileChooser.APPROVE__OPTION) { File selectedFile = jfc.getSelectedFile(); System.out.println(selectedFile.getAbsolutePath()); }
}
}
`showOpenDialog()`と `showSaveDialog()`の2つのメソッドが似ていることに注目してください。違いは、開発者がそれぞれをどのように扱うかです。読みやすくするために、私は2つの方法を混ぜることを提案しません。 出力: image://wp-content/uploads/2016/11/swing-JFileChooser-4a.png[swing-jfilechooser-4a] ユーザーがディレクトリに移動してファイルを選択し、「開く」をクリックすると、 image://wp-content/uploads/2016/11/swing-JFileChooser-4b.png[swing-jfilechooser-4b] 出力:
C:\Users\Public\Pictures\pollock.she-wolf.jpg
=== 2. setFileSelectionMode(int) - __ファイルまたはディレクトリの選択__ このメソッドでは、ディレクトリのみ( `JFileChooser.DIRECTORIES__ONLY`)またはファイルのみ(` JFileChooser.FILES__ONLY`)またはファイルとディレクトリ( `JFileChooser.FILES__AND__DIRECTORIES`)のいずれかを選択することができます。デフォルトは `FILES__ONLY`です。 `JFileChooser.DIRECTORIES__ONLY`を実装した例を次に示します。 FileChooser2.java
package com.techfou.jfileChooser;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
public class FileChooser2 {
public static void main(String[]args) {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()); jfc.setDialogTitle("Choose a directory to save your file: "); jfc.setFileSelectionMode(JFileChooser.DIRECTORIES__ONLY);
int returnValue = jfc.showSaveDialog(null); if (returnValue == JFileChooser.APPROVE__OPTION) { if (jfc.getSelectedFile().isDirectory()) { System.out.println("You selected the directory: " + jfc.getSelectedFile()); } }
}
}
出力: image://wp-content/uploads/2016/11/swing-JFileChooser-4c.png[swing-jfilechooser-4c]
You selected the directory: C:\Users\Public\Pictures
=== 3. setMultiSelectionEnabled(ブール値) - __複数選択を許可__ 複数の選択が有効になっている例ユーザーが複数のファイルを選択し、プログラムが名前を表示します。 FileChooser3.java
package com.mkyong.jfileChooser;
import java.io.File;
import java.util.Arrays;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
public class FileChooser3 {
public static void main(String[]args) {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()); jfc.setDialogTitle("Multiple file and directory selection:"); jfc.setMultiSelectionEnabled(true); jfc.setFileSelectionMode(JFileChooser.FILES__AND__DIRECTORIES);
int returnValue = jfc.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE__OPTION) { File[]files = jfc.getSelectedFiles(); System.out.println("Directories found\n"); Arrays.asList(files).forEach(x -> { if (x.isDirectory()) { System.out.println(x.getName()); } }); System.out.println("\n- - - - - - - - - - -\n"); System.out.println("Files Found\n"); Arrays.asList(files).forEach(x -> { if (x.isFile()) { System.out.println(x.getName()); } }); }
}
}
出力: image://wp-content/uploads/2016/11/swing-JFileChooser-4d.png[swing-jfilechooser-4d]
Directories found
Camera Roll
Saved Pictures
-
– – – – – – – – – –
Files Found
autumn__rhythm-pollock1.jpg
kuNUfO.jpg
mona.jpg
=== 4.フィルタ - __表示されたファイルのセットをuser__に限定します__ ユーザーの選択をプログラムのニーズに制限することは常に便利です。例えば、あなたのプログラムがpngとgifイメージを必要とするならば、ユーザの選択をそれだけに制限することをお勧めします。以下の例はカスタム `FileNameExtensionFilter`を使ってそれを達成する方法を示しています: FileChooser4.java
package com.mkyong.jfileChooser;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
public class FileChooser4 {
public static void main(String[]args) {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()); jfc.setDialogTitle("Select an image"); jfc.setAcceptAllFileFilterUsed(false); FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG and GIF images", "png", "gif"); jfc.addChoosableFileFilter(filter);
int returnValue = jfc.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE__OPTION) { System.out.println(jfc.getSelectedFile().getPath()); }
}
}
出力: image://wp-content/uploads/2016/11/swing-JFileChooser-4e.png[swing-jfilechooser-4e] あなたが見ることができるように、ユーザーは他のものを選ぶことができません。上記のディレクトリには他の種類の画像も含まれていますが、gifとpngのみがユーザーに表示されます。 ディレクトリは次のようになります。 image://wp-content/uploads/2016/11/swing-JFileChooser-4f.png[swing-jfilechooser-4f] === 5. showDialog()の使用 承認ボタンをカスタマイズする必要がある場合は、 `showDialog()`メソッドを使用してください。使用方法の例を次に示します。 FileChooser5.java
package com.mkyong.inputDialog;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
public class FileChooser5 {
public static void main(String[]args) {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()); jfc.setDialogTitle("Custom button");
int returnValue = jfc.showDialog(null, "A button!"); if (returnValue == JFileChooser.APPROVE__OPTION) { System.out.println(jfc.getSelectedFile().getPath()); }
}
}
出力: image://wp-content/uploads/2016/11/swing-JFileChooser-4g.png[swing-jfilechooser-4g] ** 注意** `JFileChooser`には` setApproveButtonText(String) `というメソッドがあります。このメソッドの問題は、 `showOpenDialog()`に対してのみ機能することです。カスタムボタンが必要な場合は、 `showSaveDialog()`の代わりに `showDialog()`を使うことをお勧めします。 また、最も簡単で一般的に使用されるファイルの書き込みと読み取りの方法を確認する必要があります。 ** link://java/file-in-java-bufferedwriter-example/に書き込む方法 Javaでファイルを書き込む - BufferedWriter]** link://java/how-to-read-file-from-java-bufferedreader-example/[How to Javaでファイルを読み込む - BufferedReader] === 参考文献 . https://docs.oracle.com/javase/8/docs/api/javax/swing/JFileChooser.html[JFileChooser - Java 8 API]。 https://docs.oracle.com/javase/8/docs/api/javax/swing/filechooser/FileSystemView.html#getFileSystemView()[FileSystemView - Java 8 API]。 https://docs.oracle.com/javase/8/docs/api/javax/swing/filechooser/FileNameExtensionFilter.html[FileNameExtension - Java 8 API] link://tag/jfilechooser/[JFileChooser]link://タグ/swing/[swing]