• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JFilechooser FileFilter

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
}

}

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic