利用JFileChooser实现文件选择对话框
简单的文件选择对话框:
/**
* 打开文件和存储文件
*/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
JLabel label = null;
JTextArea textarea = null;
JFileChooser fileChooser = null;
f = new JFrame("FileChooser Example");
Container contentPane = f.getContentPane();
textarea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textarea);
scrollPane.setPreferredSize(new Dimension(350, 300));
JButton b1 = new JButton("新建文件");
b1.addActionListener(this);
JButton b2 = new JButton("存储文件");
b2.addActionListener(this);
panel.add(b1);
panel.add(b2);
contentPane.add(scrollPane, BorderLayout.CENTER);
contentPane.add(panel, BorderLayout.SOUTH);
f.setVisible(true);
f.addWindowListener(new WindowListener() {
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
public void windowClosing(WindowEvent e) {
System.exit(0);
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
});
new FileChooserUtils();
}
File file = null;
int result;
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("打开文件");
result = fileChooser.showOpenDialog(f);
textarea.setText("");
if (result == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
label.setText("您选择打开的文件名称为:" + file.getName());
} else if (result == JFileChooser.CANCEL_OPTION) {
label.setText("您没有选择任何文件");
}
try {
fileInStream = new FileInputStream(file);
} catch (FileNotFoundException e2) {
label.setText("File Not Found");
return;
}
while ((readbyte = fileInStream.read()) != -1) {
textarea.append(String.valueOf((char) readbyte));
}
} catch (IOException e2) {
label.setText("读取文件错误");
} finally {
try {
if (fileInStream != null)
fileInStream.close();
} catch (IOException e3) {
}
}
if (e.getActionCommand().equals("存储文件")) {
result = fileChooser.showSaveDialog(f);
file = null;
String fileName;
file = fileChooser.getSelectedFile();
label.setText("您选择存储的文件名称为:" + file.getName());
} else if (result == JFileChooser.CANCEL_OPTION) {
label.setText("您没有选择任何文件");
}
try {
fileOutStream = new FileOutputStream(file);
} catch (FileNotFoundException e2) {
label.setText("File Not Found");
return;
}
fileOutStream.write(content.getBytes());
} catch (IOException e2) {
label.setText("写入文件错误");
} finally {
try {
if (fileOutStream != null)
fileOutStream.close();
} catch (IOException e3) {
}
}
}
/**
* 文件过滤
*/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.filechooser.FileFilter;
JFrame f = null;
JLabel label = null;
JFileChooser fileChooser = null;
public FileFilterUtils() {
f= new JFrame("FileFilter");
Container contentPane = f.getContentPane();
JButton b = new JButton("打开文件");
b.addActionListener(this);
label = new JLabel(" ",JLabel.CENTER);
label.setPreferredSize(new Dimension(150, 30));
contentPane.add(label,BorderLayout.CENTER);
contentPane.add(b,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
});
}
public static void main(String[] args) {
new FileFilterUtils();
public void actionPerformed(ActionEvent e) {
fileChooser = new JFileChooser("c:\\");
fileChooser.addChoosableFileFilter(new JAVAFileFilter("class"));
fileChooser.addChoosableFileFilter(new JAVAFileFilter("java"));
int result = fileChooser.showOpenDialog(f);
if(result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
label.setText("您选择了:"+file.getName()+"文件");
}else if(result == fileChooser.CANCEL_OPTION) {
label.setText("您没有选取文件");
}
}
}
class JAVAFileFilter extends FileFilter{
String ext;
public JAVAFileFilter(String ext) {
this.ext = ext;
}
public boolean accept(File file) {
if(file.isDirectory())
return true;
String fileName = file.getName();
int index = fileName.lastIndexOf('.');
if(index > 0&& index<fileName.length()-1) {
String extension = fileName.substring(index+1).toLowerCase();
if(extension.equals(ext))
return true;
}
return false;
}
public String getDescription() {
if(ext.equals("java"))
return "JAVA Source File (*.java)";
if(ext.equals("class"))
return "JAVA Source File (*.class)";
return "";
}
}
利用JFileChooser实现文件选择对话框的更多相关文章
- 文件选择对话框:CFileDialog
程序如下: CString FilePathName; //文件名参数定义 CFileDialog Dlg(TRUE,NULL,NULL, ...
- NX二次开发-UFUN文件选择对话框UF_UI_create_filebox
NX11+VS2013 #include <uf.h> #include <uf_ui.h> UF_initialize(); //文件选择对话框 char sPromptSt ...
- VBScript - 弹出“文件选择对话框”方法大全!
本文记录,VBScript 中,各种打开 "文件选择对话框" 的方法. 实现方法-1 (mshta.exe): 首先,我们要实现的就是,弹出上面的这个"文件选择对话框&q ...
- Java Swing提供的文件选择对话框 - JFileChooser
JFileChooser() 构造一个指向用户默认目录的 JFileChooser. JFileChooser(File currentDirectory) 使 ...
- Java文件选择对话框(文件选择器JFileChooser)的使用:以一个文件加密器为例
文件加密器,操作过程肯定涉及到文件选择器的使用,所以这里以文件加密器为例.下例为我自己写的一个文件加密器,没什么特别的加密算法,只为演示文件选择器JFileChooser的使用. 加密器界面如图: 项 ...
- JFileChooser 打开文件选择(一)
import javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; public clas ...
- SWT的文件选择对话框I的使用
swt文件选择框 FileDialog fileselect=new FileDialog(shell,SWT.SINGLE); fileselect ...
- DevExpress的TreeList实现自定义右键菜单打开文件选择对话框
场景 DevExpress的TreeList实现节点上添加自定义右键菜单并实现删除节点功能: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/det ...
- C# 文件选择对话框,Unity3d文件保存对话框
using OpenWinForm = System.Windows.Forms; 在unity3d中,使用FileDialog应该把System.Windows.Forms.dll拷贝到unity工 ...
随机推荐
- Django框架(二十)—— Django rest_framework-认证组件
目录 Django rest_framework-认证组件 一.什么是认证 二.利用token记录认证过的用户 1.什么是token 2.token的原理 3.cookie.session.token ...
- 调用WPF程序时传入参数
http://blog.csdn.net/martin_cheng/article/details/41351013 http://blog.csdn.net/libby1984/article/de ...
- this与super的语法比较
this 代表当前对象 可以代表当前属性,当前方法,当前对象(整个自己). 作用:解决同名变量的同名问题,同明变量可能来源于父类,局部变量和成员变量... 语法使用:this( 实参... ); 调用 ...
- CentOS6.5源码安装MySQL5.6.35
CentOS6.5源码安装MySQL5.6.35 一.卸载旧版本 1.使用下面的命令检查是否安装有mysql [root@localhost tools]# rpm -qa|grep -i mysql ...
- 在Python中检测*可用* CPU数量的便携方式
根据这个问题和答案 - Python multiprocessing.cpu_count()在4核Nvidia Jetson TK1上返回'1' - Python multiprocessing.cp ...
- 纯Delphi 原生写的 上传到七牛的功能
上传文件到七牛, 支持分片分段上传, 适用于Delphi XE, 10等新版本 分两个函数: uploadToQiniu 和 directUploadToQiniu uploadToQiniu 这个函 ...
- 【总】IdentityServer4 32篇汇总
随笔分类 - IdentityServer4 IdentityServer4 常见问题 - 用户拒绝授权后报错 摘要: 1.问题说明 在 IdentityServer4 Web 授权中,一般会有一个显 ...
- subst - 替换文件中的定义
总览 (SYNOPSIS) subst [ -e editor ] -f substitutions victim ... 描述 (DESCRIPTION) Subst 能够 替换 文件 的 内容, ...
- mysql-视图及索引简介
一.视图的创建.作用及注意事项 1.创建:create view 视图名 as select 语句: 2.删除:drop view 视图名 3.作用: 数据库视图允许简化复杂查询 数据库视图有助于限制 ...
- 使用vscode搭建本地的websocket
首先在服务器方面,网上都有不同的对websocket支持的服务器: php - http://code.google.com/p/phpwebsocket/ jetty - http://jetty. ...