TOP →
Java →
Swing →
JFileChooser → This Page
JFileChooser@Swing サンプル03
概要
Java -
Swing -
JFileChooser のサンプルです。
・ダイアログタイトル変更
・隠しファイル表示
・選択モードを設定
解説
ボタン1はタイトルを変更したダイアログを表示、
ボタン2は隠しファイルを表示したダイアログを表示、
ボタン3はディレクトリのみ選択可能なダイアログを表示、
ボタン4はファイルもディレクトリも選択可能なダイアログを表示します。
選択モードの設定には setFileSelectionMode メソッドを利用します。
パラメータは以下の通りです。
パラメータ | 内容 |
JFileChooser.FILES_ONLY |
ファイルのみ選択可能 |
JFileChooser.DIRECTORIES_ONLY |
ディレクトリのみ選択可能 |
JFileChooser.FILES_AND_DIRECTORIES |
ファイルもディレクトリも選択可能 |
サンプルイメージ
ダイアログのタイトルを変更しています。
ディレクトリのみ選択可能なダイアログです。
サンプルソース
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 サンプル03
* ・ダイアログタイトル変更
* ・隠しファイル表示
* ・選択モードを設定
*
* @author みっちー
*/
public class JFileChooser03 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) {
JFileChooser03 frame = new JFileChooser03();
// 閉じるボタンをクリックされた場合の動作を設定
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ウインドウのタイトルを設定
frame.setTitle("JFileChooser サンプル03");
// フレームの X座標、Y座標、幅、高さを設定
frame.setBounds(100, 200, 480, 120);
// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
frame.setVisible(true);
}
/**
* コンストラクタ
*/
public JFileChooser03() {
// パネルを作成
JPanel panelBase = new JPanel();
DefaultColorSelectionModel mode = new DefaultColorSelectionModel();
mode.setSelectedColor(Color.RED);
// ボタン作成
button1 = new JButton("Dialog Title");
button2 = new JButton("Hidden File");
button3 = new JButton("Only Directory");
button4 = new JButton("File and Directory");
// ラベル
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)) {
// 開くダイアログ表示
// ダイアログタイトル変更
fc.setDialogTitle("好きなファイルを開きましょう");
} else if (e.getSource().equals(button2)) {
// 開くダイアログ表示
// 隠しファイル表示
fc.setFileHidingEnabled(false);
} else if (e.getSource().equals(button3)) {
// 開くダイアログ表示
// 選択モードを設定(ディレクトリのみ)
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
} else if (e.getSource().equals(button4)) {
// 開くダイアログ表示
// 選択モードを設定(ファイルとディレクトリ)
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
}
int selected = fc.showOpenDialog(this);
if (selected == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
label.setText(file.getAbsolutePath());
}
}
}
サンプルソースのダウンロード
ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)
更新履歴
2016/05/13 新規作成
TOP →
Java →
Swing →
JFileChooser → This Page