是这样的,我想在用FileChooser选择打开文件的时候做个文件过滤,比如我想打开.txt的文件,就这样,但是我试了一些方法好像不行,知道的能告诉我下么?,我的代码贴出来:知道的帮我跑出来啊,要跑出来的,谢谢。
@Override
public void mouseClicked(MouseEvent e) {
doOpenfile();
} private void doOpenfile() {
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogTitle("打开文件夹");
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int ret = fileChooser.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION) {
//文件夹路径
System.out.println(fileChooser.getSelectedFile().getAbsolutePath());
jtfpath.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
}
@Override
public void mouseClicked(MouseEvent e) {
doOpenfile();
} private void doOpenfile() {
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogTitle("打开文件夹");
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int ret = fileChooser.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION) {
//文件夹路径
System.out.println(fileChooser.getSelectedFile().getAbsolutePath());
jtfpath.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
}
解决方案 »
- 急急急!jdom 解析xml文件报错
- java中绘图是不是可以覆盖任何组件的?
- java 用JDBC向MYSQL中建立表,求高速
- 求助关于一个关于网络编程的问题
- java连接池的问题
- 一个关于InputStreamReader的编码问题
- 请问我的APPLET程序怎么在WEB上调用啊~~~?
- java有什么版本控制工具吗?
- 有一个jar文件,我把它解压缩,然后修改了MANIFEST.MF里面的一些内容,我想把这些文件再从新打包城jar文件,怎么办??
- 在javaSE里面,为什么要使用泛型通配符呢?
- java中的Dialog里面设置setModel的true和false有上面区别
- 帮忙分析一段多线程的代码
覆盖里面的accept方法和getDescription方法
然后fileChooser.addChoosableFileFilter(new Filter())
fileChooser.setFileFilter(new FileFilter(){
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String ext = null;
String s = f.getName();
int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) {
ext = s.substring(i+1).toLowerCase();
}
if (ext != null) {
if (ext.equals("txt")) {
return true;
} else {
return false;
}
}
return false;
}
//The description of this filter
public String getDescription() {
return "Just Txt";
}
});
package com.spdb.swing;import java.io.File;
import javax.swing.filechooser.*;/* ImageFilter.java is used by FileChooserDemo2.java. */
public class TextFilter extends FileFilter {
/*
* Get the extension of a file.
*/
public static String getExtension(File f) {
String ext = null;
String s = f.getName();
int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) {
ext = s.substring(i+1).toLowerCase();
}
return ext;
}
//Accept all directories and all gif, jpg, tiff, or png files.
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
} String extension = getExtension(f);
if (extension != null) {
if (extension.equals("txt") ||
extension.equals("dat")) {
return true;
} else {
return false;
}
} return false;
} //The description of this filter
public String getDescription() {
return "Just Text Files";
}
}
使用方法:// Show it.
int returnVal = fc.showDialog(RelationImportPanel.this, "打开"); // Process the results.
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
txtFilePath.setText(file.getAbsolutePath());
log.debug("Attaching file: " + file.getName() + ".");
} else {
log.debug("Attachment cancelled by user.");
} // Reset the file chooser for the next time it's shown.
fc.setSelectedFile(null);
http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html