package lecture.swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
import jp.avaj.lib.debug.L;
import jp.avaj.lib.swing.PcScreen;
import jp.avaj.lib.swing.SwingComp;
/**
* JFileChooserの使用サンプル Swing Java
*/
class FileChooserTest extends JFrame {
public static void main(String[] args) {
new FileChooserTest();
}
private FileChooserTest() {
setTitle("JFileChooser Test");
JPanel mainPanel = new JPanel();
getContentPane().add(mainPanel);
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
//
// ファイル選択をREADモードで行う
{
final String thema = "select file for read";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// READモードで表示する
int result = chooser.showOpenDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// ファイル選択をWRITEモードで行う
{
final String thema = "select file for write";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// WRITEモードで表示する
int result = chooser.showSaveDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// ディレクトリ選択をREADモードで行う
{
final String thema = "select directory for read";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// READモードで表示する
int result = chooser.showOpenDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// ディレクトリ選択をWRITEモードで行う
{
final String thema = "select directory for write";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// WRITEモードで表示する
int result = chooser.showSaveDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// ファイルまたはディレクトリ選択をREADモードで行う
{
final String thema = "select file,directory for read";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
// READモードで表示する
int result = chooser.showOpenDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// ファイルまたはディレクトリ選択をWRITEモードで行う
{
final String thema = "select file,directory for write";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
// WRITEモードで表示する
int result = chooser.showSaveDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// 複数ファイル選択をREADモードで行う
{
final String thema = "select files for read";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// 複数ファイル選択の指定
chooser.setMultiSelectionEnabled(true);
// READモードで表示する
int result = chooser.showOpenDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File[] selectedFiles = chooser.getSelectedFiles();
for (File selectedFile : selectedFiles) {
L.p(selectedFile.getAbsolutePath());
}
}
}
);
}
//
// ファイルのフィルタリング
// javaファイルとtxtファイルを指定してREADモードで行う
{
final String thema = "select java,txt file for read";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// javaファイルとtxtファイルを指定する
// 第一引数は説明、以降は可変個引数でファイルタイプを指定する
// 注意、これ以上のフィルタリングはjavax.swing.filechooser.FileFilterを継承して自分で実装する必要がある
chooser.setFileFilter(new FileNameExtensionFilter("Javaまたはtxtファイル", "java","txt"));
// READモードで表示する
int result = chooser.showOpenDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// 画面を表示する
pack();
PcScreen.display(this,PcScreen.POS_CENTER_CENTER);
setVisible(true);
}
//
// private constants and variables
}
//---------------------------------------------------
//・目次 - Swing
// http://blogs.yahoo.co.jp/artery2020/40568561.html
//・目次 - Java入門
// http://blogs.yahoo.co.jp/artery2020/39975776.html
//・目次 - ビジネスパーソンの常識と非常識
// http://blogs.yahoo.co.jp/artery2020/39728331.html
//・目次 - 論理・発想・思考についての考察と鍛え方
// http://blogs.yahoo.co.jp/artery2020/39657784.html
//---------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
import jp.avaj.lib.debug.L;
import jp.avaj.lib.swing.PcScreen;
import jp.avaj.lib.swing.SwingComp;
/**
* JFileChooserの使用サンプル Swing Java
*/
class FileChooserTest extends JFrame {
public static void main(String[] args) {
new FileChooserTest();
}
private FileChooserTest() {
setTitle("JFileChooser Test");
JPanel mainPanel = new JPanel();
getContentPane().add(mainPanel);
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
//
// ファイル選択をREADモードで行う
{
final String thema = "select file for read";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// READモードで表示する
int result = chooser.showOpenDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// ファイル選択をWRITEモードで行う
{
final String thema = "select file for write";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// WRITEモードで表示する
int result = chooser.showSaveDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// ディレクトリ選択をREADモードで行う
{
final String thema = "select directory for read";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// READモードで表示する
int result = chooser.showOpenDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// ディレクトリ選択をWRITEモードで行う
{
final String thema = "select directory for write";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// WRITEモードで表示する
int result = chooser.showSaveDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// ファイルまたはディレクトリ選択をREADモードで行う
{
final String thema = "select file,directory for read";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
// READモードで表示する
int result = chooser.showOpenDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// ファイルまたはディレクトリ選択をWRITEモードで行う
{
final String thema = "select file,directory for write";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
// WRITEモードで表示する
int result = chooser.showSaveDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// 複数ファイル選択をREADモードで行う
{
final String thema = "select files for read";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// 複数ファイル選択の指定
chooser.setMultiSelectionEnabled(true);
// READモードで表示する
int result = chooser.showOpenDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File[] selectedFiles = chooser.getSelectedFiles();
for (File selectedFile : selectedFiles) {
L.p(selectedFile.getAbsolutePath());
}
}
}
);
}
//
// ファイルのフィルタリング
// javaファイルとtxtファイルを指定してREADモードで行う
{
final String thema = "select java,txt file for read";
JButton btn = SwingComp.button(thema,mainPanel,(ActionListener)null);
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(thema);
// 初期表示ディレクトリの指定
chooser.setCurrentDirectory(new File("."));
// ファイルorディレクトリの指定
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// javaファイルとtxtファイルを指定する
// 第一引数は説明、以降は可変個引数でファイルタイプを指定する
// 注意、これ以上のフィルタリングはjavax.swing.filechooser.FileFilterを継承して自分で実装する必要がある
chooser.setFileFilter(new FileNameExtensionFilter("Javaまたはtxtファイル", "java","txt"));
// READモードで表示する
int result = chooser.showOpenDialog(FileChooserTest.this);
//
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
L.p(selectedFile.getAbsolutePath());
}
}
);
}
//
// 画面を表示する
pack();
PcScreen.display(this,PcScreen.POS_CENTER_CENTER);
setVisible(true);
}
//
// private constants and variables
}
//---------------------------------------------------
//・目次 - Swing
// http://blogs.yahoo.co.jp/artery2020/40568561.html
//・目次 - Java入門
// http://blogs.yahoo.co.jp/artery2020/39975776.html
//・目次 - ビジネスパーソンの常識と非常識
// http://blogs.yahoo.co.jp/artery2020/39728331.html
//・目次 - 論理・発想・思考についての考察と鍛え方
// http://blogs.yahoo.co.jp/artery2020/39657784.html
//---------------------------------------------------