タイトル
TOPJavaSwingJFileChooser → This Page

JFileChooser@Swing サンプル02

概要

Java - Swing - JFileChooser のサンプルです。
・開くダイアログ表示
・保存ダイアログ表示
・任意ダイアログ表示

解説

開くダイアログ、保存ダイアログ、そして任意の文字列を持つダイアログを表示します。
それぞれのダイアログの戻り値は以下の通りです。
戻り値内容
JFileChooser.APPROVE_OPTION ファイルまたはディレクトリを選択された
JFileChooser.CANCEL_OPTION 「取消」を選択された
JFileChooser.ERROR_OPTION エラーが発生した

サンプルイメージ

サンプル画像

開くダイアログです。
サンプル画像

保存ダイアログです。
サンプル画像

任意の文字列を持つダイアログです。
サンプル画像


サンプルソース

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;

/**
 * JFileChooser サンプル02
 * ・開くダイアログ表示
 * ・保存ダイアログ表示
 * ・任意ダイアログ表示
 * 
 * @author みっちー
 */
public class JFileChooser02 extends JFrame implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	/** ボタン **/
	private JButton button1 = null;
	private JButton button2 = null;
	private JButton button3 = null;
	
	/** ラベル **/
	private JLabel label = null;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JFileChooser02 frame = new JFileChooser02();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JFileChooser サンプル02");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 400, 120);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JFileChooser02() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		
		DefaultColorSelectionModel mode = new DefaultColorSelectionModel();
		mode.setSelectedColor(Color.RED);
		
		// ボタン作成
		button1 = new JButton("開く");
		button2 = new JButton("保存");
		button3 = new JButton("任意");
		
		// ラベル
		label = new JLabel();
		Border border = new BevelBorder(BevelBorder.LOWERED);
		label.setBorder(border);
		label.setPreferredSize(new Dimension(360, 30));
		
		// アクションリスナー追加
		button1.addActionListener(this);
		button2.addActionListener(this);
		button3.addActionListener(this);
		
		// ボタンを追加
		panelBase.add(button1);
		panelBase.add(button2);
		panelBase.add(button3);
		panelBase.add(label);
		
		// パネルを追加
		getContentPane().add(panelBase);
	}
	
	/**
	 * アクション
	 */
	public void actionPerformed(ActionEvent e) {
		// ファイル選択を作成&表示
		JFileChooser fc = new JFileChooser();
		if (e.getSource().equals(button1)) {
			// 開くダイアログ表示
			int selected = fc.showOpenDialog(this);
			if (selected == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				label.setText(file.getAbsolutePath());
			} else if (selected == JFileChooser.CANCEL_OPTION) {
				label.setText("取り消し");
			} else if (selected == JFileChooser.ERROR_OPTION) {
				label.setText("エラー");
			}
		} else if (e.getSource().equals(button2)) {
			// 保存ダイアログ表示
			int selected = fc.showSaveDialog(this);
			if (selected == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				label.setText(file.getAbsolutePath());
			} else if (selected == JFileChooser.CANCEL_OPTION) {
				label.setText("取り消し");
			} else if (selected == JFileChooser.ERROR_OPTION) {
				label.setText("エラー");
			}
		} else if (e.getSource().equals(button3)) {
			// 任意ダイアログ表示
			int selected = fc.showDialog(this, "任意の文字列");
			if (selected == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				label.setText(file.getAbsolutePath());
			} else if (selected == JFileChooser.CANCEL_OPTION) {
				label.setText("取り消し");
			} else if (selected == JFileChooser.ERROR_OPTION) {
				label.setText("エラー");
			}
		}
	}
}

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

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

更新履歴

2016/05/13 新規作成


TOPJavaSwingJFileChooser → This Page