Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JFilechooser FileFilter  RSS feed

 
Anant Desai
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to set up a file filter in Swing that
looks for filenames beginning with certain characters. For example,
Credit filenames beging with "ECS" and debit with "ED". I have two
Jbuttons in my Frame which allow to select either ALL credit files or
ALL debit files. My program is attached. The code throws out a compile error.

public class OnlyCredit {

private String extension;
private String description;

public OnlyCredit(String extension,String description) {
this.filter = new FileFilter() {
@Override
public boolean accept(File f) {
return f.getName().startsWith("ECS");
}

@Override
public String getDescription() {
return description;
}
};
this.extension = extension;
this.description = description;

throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

FileFilter filter;

OnlyCredit(String ext, String desc) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anant Desai wrote: The code throws out a compile error.

Since we can't guess what the error is and our crystal balls are down for repairs please copy paste the error stack trace. (Just don't forget to UseCodeTags )
 
Anant Desai
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have since modified the code as follows:

import java.io.File;
import javax.swing.filechooser.FileFilter;

public class OnlyCredit extends FileFilter {

private String description;

public OnlyCredit(String description) {
//(this.filter = new FileFilter() {)
this.description = description;
//@Override
//public String getDescription() {
// return description;
//}
// };
//};
}

@Override
public boolean accept(File f) {
System.out.println(f.getName());
return f.getName().startsWith("ECS");
}

@Override
public String getDescription() {
return description;

}
}
I have used JFileChooser.addChooseableFileFilter with this filter,
but it returns ALL files in the selected directory.
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've added the file filter, but is it also the currently selected one? By default, a file filter for all files is added, and if you don't remove it (there is a method in JFileChooser for this) or select another one (again, there is a method), it's the default selection.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh asked you to use the code tags. You are still not using the code tags so I won't take the time to read unformatted code. Maybe next time.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!