求助:在用openFileDialog打开文件的时候如何只显示特定类型的文件?

smcrescent 2004-07-26 09:42:29
还有能不能把某个文件夹下面所有的特定文件类型的文件都打开??
能不能把某个文件夹和子文件夹下面所有的特定文件都打开???
谢谢了

分不够再开帖子加~~
谢谢谢谢~~
...全文
518 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
minghuitian 2004-07-26
  • 打赏
  • 举报
回复
up
tomcatjava 2004-07-26
  • 打赏
  • 举报
回复
/**
* Customize the FileView which can link specifid files to icons accordingly
*
*/
class ShareFileView extends FileView {
private HashMap hash = new HashMap();

public ShareFileView() {
hash.put( "html",new AnOvalIcon( Color.red ) );
hash.put( "htm",new AnOvalIcon( Color.red ) );
hash.put( "xhtml",new AnOvalIcon( Color.red ) );
}

public String getName( File f ) {
String s = f.getName();
if( s.length() == 0 ) {
s = f.getAbsolutePath();
}
return s;
}

public String getDescription( File f ) {
return f.getName();
}

public String getTypeDescription( File f ) {
return f.getAbsolutePath();
}

public Icon getIcon( File f ) {
String path = f.getAbsolutePath();
int pos = path.lastIndexOf('.');
if( ( pos >= 0 ) && ( pos < ( path.length()-1 ) ) ) {
String ext = path.substring( pos+1 ).toLowerCase();
return (Icon)hash.get( ext );
}
return null;
}

public Boolean isTraversable( File file ) {
return ( new Boolean( file.isDirectory() ) );
}
}

class ShareFileFilter extends FileFilter {
private String extensions[];
private String description;

public ShareFileFilter( String description,String extension ) {
this( description, new String[] { extension } );
}

public ShareFileFilter( String description,String extensions[] ) {
this.description = description;
this.extensions = (String[])extensions.clone();
}

public boolean accept( File file ) {
if( file.isDirectory() ){
return true;
}
int count = extensions.length;
String path = file.getAbsolutePath();
for( int i = 0; i < count ; i++ ) {
String ext = extensions[i];
if( path.endsWith( ext ) &&
( path.charAt( path.length()-ext.length() ) == '.' ) ) {
return true;
}
}
return false;
}

public String getDescription() {
return ( description == null? extensions[0]:description );
}
}


然后:
chooser = new JFileChooser( "." );
FileFilter type = new ShareFileFilter( "html files",
new String[]{ ".htm",".html","xhtml" } );
chooser.addChoosableFileFilter( type );
chooser.setFileFilter( type );
FileView view = new ShareFileView();
chooser.setFileView( view );
lixiang823517 2004-07-26
  • 打赏
  • 举报
回复
class ThisFileFilter extends FileFilter {

String ext;
public ThisFileFilter(String ext) {
this.ext=ext;
}
/* (非 Javadoc)
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
*/
public boolean accept(File file) {
// TODO 自动生成方法存根
if (file.isDirectory()) {
return true;
}
String fileName = file.getName();
int index = fileName.lastIndexOf(".");
if (index > 0 && index < fileName.length() - 1) {
String extendsion = fileName.substring(index + 1).toLowerCase();
if (extendsion.equals(ext)) {
return true;
}
}
return false;
}

/* (非 Javadoc)
* @see javax.swing.filechooser.FileFilter#getDescription()
*/
public String getDescription() {
// TODO 自动生成方法存根
if(ext.equals("txt"))
{
return "文本文件(.txt)";
}
if(ext.equals("xls"))
{
return "EXCEL文件(.xls)";
}
return "";
}

}

实现FileFilter接口,这两个方法是必须覆写的

在调用的时候
jfc.addChoosableFileFilter(new ThisFileFilter("txt"));
jfc.addChoosableFileFilter(new ThisFileFilter("xls"));
zhigangsun 2004-07-26
  • 打赏
  • 举报
回复
对呀,实现FileFilter接口呀.你自己可以定义要显示的扩展名.
maowu 2004-07-26
  • 打赏
  • 举报
回复
class EDBFileFilter
extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
if (file.isDirectory() || file.getPath().toLowerCase().endsWith(".xml"))
return true;
else
return false;
}

public String getDescription() {
return "Eproms xml files(.xml)";
}
}
JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new EDBFileFilter());

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧