TOP →
Java →
Swing →
JFileChooser → This Page
JFileChooser@Swing サンプル06
概要
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.Action;
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 サンプル06
* ・詳細表示
*
* @author みっちー
*/
public class JFileChooser06 extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
/** ラベル **/
private JLabel label = null;
/**
* 開始メソッド
*
* @param args パラメータ
*/
public static void main(String[] args) {
JFileChooser06 frame = new JFileChooser06();
// 閉じるボタンをクリックされた場合の動作を設定
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ウインドウのタイトルを設定
frame.setTitle("JFileChooser サンプル06");
// フレームの X座標、Y座標、幅、高さを設定
frame.setBounds(100, 200, 480, 120);
// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
frame.setVisible(true);
}
/**
* コンストラクタ
*/
public JFileChooser06() {
// パネルを作成
JPanel panelBase = new JPanel();
DefaultColorSelectionModel mode = new DefaultColorSelectionModel();
mode.setSelectedColor(Color.RED);
// ボタン作成
JButton button = new JButton("詳細表示");
// ラベル
label = new JLabel();
Border border = new BevelBorder(BevelBorder.LOWERED);
label.setBorder(border);
label.setPreferredSize(new Dimension(420, 30));
// アクションリスナー追加
button.addActionListener(this);
// ボタンを追加
panelBase.add(button);
panelBase.add(label);
// パネルを追加
getContentPane().add(panelBase);
}
/**
* アクション
*/
public void actionPerformed(ActionEvent e) {
// 詳細表示
JFileChooser fc = new JFileChooser();
Action detailsAction = fc.getActionMap().get("viewTypeDetails");
if (detailsAction != null) {
detailsAction.actionPerformed(null);
}
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