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

JFileChooser@Swing サンプル01

概要

Java - Swing - JFileChooser のサンプルです。
・色々なコンストラクタ

解説

色々なコンストラクタを使用してファイル選択を作成しています。

サンプルイメージ

サンプル画像

ファイル選択ダイアログを表示します。
サンプル画像


サンプルソース

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.FileSystemView;

/**
 * JFileChooser サンプル01
 * ・色々なコンストラクタ
 * 
 * @author みっちー
 */
public class JFileChooser01 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 JButton button5 = null;
	private JButton button6 = null;
	
	/** ラベル **/
	private JLabel label = null;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JFileChooser01 frame = new JFileChooser01();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JFileChooser サンプル01");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 400, 160);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JFileChooser01() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		
		DefaultColorSelectionModel mode = new DefaultColorSelectionModel();
		mode.setSelectedColor(Color.RED);
		
		// ボタン作成
		button1 = new JButton("ボタン1");
		button2 = new JButton("ボタン2");
		button3 = new JButton("ボタン3");
		button4 = new JButton("ボタン4");
		button5 = new JButton("ボタン5");
		button6 = new JButton("ボタン6");
		
		// ラベル
		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);
		button4.addActionListener(this);
		button5.addActionListener(this);
		button6.addActionListener(this);
		
		// ボタンを追加
		panelBase.add(button1);
		panelBase.add(button2);
		panelBase.add(button3);
		panelBase.add(button4);
		panelBase.add(button5);
		panelBase.add(button6);
		panelBase.add(label);
		
		// パネルを追加
		getContentPane().add(panelBase);
	}
	
	/**
	 * アクション
	 */
	public void actionPerformed(ActionEvent e) {
		// ファイル選択を作成&表示
		JFileChooser fc = null;
		if (e.getSource().equals(button1)) {
			fc = new JFileChooser();
		} else if (e.getSource().equals(button2)) {
			fc = new JFileChooser(new File("C://"));
		} else if (e.getSource().equals(button3)) {
			FileSystemView fsv = FileSystemView.getFileSystemView();
			fc = new JFileChooser(new File("C://"), fsv);
		} else if (e.getSource().equals(button4)) {
			FileSystemView fsv = FileSystemView.getFileSystemView();
			fc = new JFileChooser(fsv);
		} else if (e.getSource().equals(button5)) {
			fc = new JFileChooser("C://");
		} else if (e.getSource().equals(button6)) {
			FileSystemView fsv = FileSystemView.getFileSystemView();
			fc = new JFileChooser("C://", fsv);
		}
		
		int selected = fc.showOpenDialog(this);
		if (selected == JFileChooser.APPROVE_OPTION){
			File file = fc.getSelectedFile();
			label.setText(file.getAbsolutePath());
		}
	}
}

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

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

更新履歴

2016/05/13 新規作成


TOPJavaSwingJFileChooser → This Page
Valid CSS!