タイトル
 メニューにないコーナーはTopからいけます
TOPJavaSwingJFileChooser → This Page

JFileChooser@Swing サンプル04

概要

Java - Swing - JFileChooser のサンプルです。
・フィルタ1
・フィルタ2
・「すべてのファイル」オフ
・Approveボタンの文字列変更
・Approveボタンにヒント

解説

ボタン1、2はフィルタ付の「開くダイアログ」を表示、
ボタン3は「すべてのファイル」がない「開くダイアログ」を表示、
ボタン4「開く」の文字列を変更し、ツールチップを設定した「開くダイアログ」を表示します。

フィルタは addChoosableFileFilter メソッドに FileFilter 型のオブジェクトをセットします。
・FileFilter クラスを拡張したクラスを利用
・FileNameExtensionFilter クラスを利用
の2通りの方法があります。

FileFilter クラスの拡張
必須メソッド
メソッドパラメータ内容
public abstract boolean accept(File f) f ファイル
戻り値 表示するファイルの場合はtrue
public abstract String getDescription() 戻り値 フィルタの説明テキストを返す

FileNameExtensionFilter クラスのコンストラクタ
コンストラクタパラメータ内容
FileNameExtensionFilter(String description, String... extensions) description フィルタの説明テキスト
extensions 受け入れるファイルの拡張子

サンプルイメージ

サンプル画像

フィルタでJPEGファイルを選択可能なダイアログです。
サンプル画像

「すべてのファイル」がないダイアログです。
サンプル画像

「開く」の文字列を変更し、ツールチップを設定したダイアログです。
サンプル画像


サンプルソース

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.colorchooser.DefaultColorSelectionModel;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * JFileChooser サンプル04
 * ・フィルタ1
 * ・フィルタ2
 * ・「すべてのファイル」オフ
 * ・Approveボタンの文字列変更
 * ・Approveボタンにヒント
 * 
 * @author みっちー
 */
public class JFileChooser04 extends JFrame implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	/** ボタン **/
	private JButton button1 = null;
	private JButton button2 = null;
	private JButton button3 = null;
	private JButton button4 = null;
	
	/** ラベル **/
	private JLabel label = null;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JFileChooser04 frame = new JFileChooser04();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JFileChooser サンプル04");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 480, 120);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JFileChooser04() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		
		DefaultColorSelectionModel mode = new DefaultColorSelectionModel();
		mode.setSelectedColor(Color.RED);
		
		// ボタン作成
		button1 = new JButton("Filter-1");
		button2 = new JButton("Filter-2");
		button3 = new JButton("No All-Files");
		button4 = new JButton("Approve Button");
		
		// ラベル
		label = new JLabel();
		Border border = new BevelBorder(BevelBorder.LOWERED);
		label.setBorder(border);
		label.setPreferredSize(new Dimension(420, 30));
		
		// アクションリスナー追加
		button1.addActionListener(this);
		button2.addActionListener(this);
		button3.addActionListener(this);
		button4.addActionListener(this);
		
		// ボタンを追加
		panelBase.add(button1);
		panelBase.add(button2);
		panelBase.add(button3);
		panelBase.add(button4);
		panelBase.add(label);
		
		// パネルを追加
		getContentPane().add(panelBase);
	}
	
	/**
	 * アクション
	 */
	public void actionPerformed(ActionEvent e) {
		// ファイル選択を作成&表示
		JFileChooser fc = new JFileChooser();
		if (e.getSource().equals(button1)) {
			// 開くダイアログ表示
			// フィルタ1(全てのファイル+JPEGのみ表示)
			fc.addChoosableFileFilter(new JpegFilter());
		} else if (e.getSource().equals(button2)) {
			// 開くダイアログ表示
			// フィルタ2(全てのファイル+JPEGのみ表示)
			FileFilter filter = new FileNameExtensionFilter("JPEGファイル", "jpg", "jpeg");
			fc.addChoosableFileFilter(filter);
		} else if (e.getSource().equals(button3)) {
			// 開くダイアログ表示
			// 「すべてのファイル」オフ
			fc.addChoosableFileFilter(new JpegFilter());
			fc.setAcceptAllFileFilterUsed(false);
		} else if (e.getSource().equals(button4)) {
			// 開くダイアログ表示
			// Approveボタンの文字列変更
			// Approveボタンにヒント
			fc.setApproveButtonText("選択ファイルを開く");
			fc.setApproveButtonToolTipText("選択ファイルを開きます。");
		}
		int selected = fc.showOpenDialog(this);
		if (selected == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			label.setText(file.getAbsolutePath());
		}
	}
}
import java.io.File;

import javax.swing.filechooser.FileFilter;

/**
 * Jpeg用フィルタ
 * 
 * @author みっちー
 */
public class JpegFilter extends FileFilter {
	
	/**
	 * acceptメソッド
	 * 
	 * @param f ファイルオブジェクト
	 */
	@Override
	public boolean accept(File f) {
		if (f.isDirectory()) {
			return true;
		}
		
		// 拡張子の取得と判別
		String ext = getExtension(f);
		if (ext != null) {
			if (ext.equals("jpg") || ext.equals("jpeg")) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}

	/**
	 * getDescriptionメソッド
	 */
	@Override
	public String getDescription() {
		return "JPEGファイル";
	}

	/**
	 * 拡張子取得
	 * 
	 * @param f ファイルオブジェクト
	 */
	private String getExtension(File f) {
		String ext = null;
		String filename = f.getName();
		int dotIndex = filename.lastIndexOf('.');
		if ((dotIndex > 0) && (dotIndex < filename.length() - 1)) {

			ext = filename.substring(dotIndex + 1).toLowerCase();
		}
		return ext;
	}
}

サンプルソースのダウンロード

ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)

更新履歴

2016/05/13 新規作成


TOPJavaSwingJFileChooser → This Page
Valid CSS!