转载 http://hi.baidu.com/gglzf4/blog/item/a950a402ff7ae67b3812bba2.html
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//只能选择文件夹
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//只能选择文件
int flag = fileChooser.showOpenDialog(this);
if(flag ==JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
attachHorizontalList.addNewAttaches(file);
jScrollPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
// jScrollPane.setBorder(BorderFactory.createTitledBorder("共"+(++count)+"份附件"));
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
jScrollPane.setOpaque(false);
jScrollPane.getViewport().setOpaque(false);
this.add(jScrollPane);
this.repaint();
}
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int flag = chooser.showSaveDialog(mainFrame);
if (flag == JFileChooser.APPROVE_OPTION) {
String path = chooser.getSelectedFile().getAbsolutePath()+attachmentName;
try {
FileInputStream ins = new FileInputStream(
attachmentFile);
FileOutputStream out = new FileOutputStream(path);
byte b[] = new byte[512];
while ((ins.read(b)) > 0) {
out.write(b);
}
out.close();
ins.close();
} catch (IOException e2) {
JOptionPane.showMessageDialog(null, "保存失败");
e2.printStackTrace();
}
}
打开对话框和保存对话框的设计 swing java
1.点击打开按钮后,打开一个对话框
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//产生一个文件选择器
jChooser = new JFileChooser();
//设置默认的打开目录,如果不设的话按照window的默认目录(我的文档)
jChooser.setCurrentDirectory(new File("e:/"));
//设置打开文件类型,此处设置成只能选择文件夹,不能选择文件
jChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//只能打开文件夹
//打开一个对话框
int index = jChooser.showDialog(null, "打开文件");
if (index == JFileChooser.APPROVE_OPTION) {
//把获取到的文件的绝对路径显示在文本编辑框中
jt.setText(jChooser.getSelectedFile().getAbsolutePath());
readPath = jt.getText() + "//";
}
}
});
2.点击保存按钮后,打开一个保存对话框
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
jChooser2 = new JFileChooser();
jChooser2.setCurrentDirectory(new File("e:/"));//设置默认打开路径
jChooser2.setDialogType(JFileChooser.SAVE_DIALOG);//设置保存对话框
//将设置好了的两种文件过滤器添加到文件选择器中来
TxtFileFilter txtFileFilter = new TxtFileFilter();
XlsFileFilter xlsFileFilter = new XlsFileFilter();
jChooser2.addChoosableFileFilter(txtFileFilter);
jChooser2.addChoosableFileFilter(xlsFileFilter);
int index = jChooser2.showDialog(null, "保存文件");
if (index == JFileChooser.APPROVE_OPTION) {
File f = jChooser2.getSelectedFile();
String fileName = jChooser2.getName(f) + ".xls";
writePath = jChooser2.getCurrentDirectory().getAbsolutePath() + fileName;
try {
writeFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//程序执行完毕后,出现一个对话框来提示
int option = JOptionPane.showConfirmDialog(null, "贱人:结果已产生", "结果",
JOptionPane.YES_NO_OPTION);
System.exit(0);
}
}
});
//重写文件过滤器,设置打开类型中几种可选的文件类型,这里设了两种,一种txt,一种xls
class TxtFileFilter extends FileFilter {
@Override
public boolean accept(File f) {
// TODO Auto-generated method stub
String nameString = f.getName();
return nameString.toLowerCase().endsWith(".txt");
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return "*.txt(文本文件)";
}
}
class XlsFileFilter extends FileFilter {
@Override
public boolean accept(File f) {
// TODO Auto-generated method stub
String nameString = f.getName();
return nameString.toLowerCase().endsWith(".xls");
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return "*.xls(表格文件)";
}
}