I have one problem in fileChooser.
when fileChooser window come then I want to show .java as a default name in “File of type” and in list I want to show *.java, *.class, All File respectively.
For this I use following code:
public class Main {
public static void main(String[] argv) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFileChooser fileChooser = new JFileChooser(new File("."));
fileChooser.addChoosableFileFilter(new MyFilter());
fileChooser.addChoosableFileFilter(new MyFilter2());
fileChooser.setAcceptAllFileFilterUsed(true);
fileChooser.showOpenDialog(null);
System.out.println(fileChooser.getSelectedFile());
}
}
class MyFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
String filename = file.getName();
return filename.endsWith(".java");
}
public String getDescription() {
return "*.java";
}
}
class MyFilter2 extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
String filename = file.getName();
return filename.endsWith(".class");
}
public String getDescription() {
return "*.class";
}
}
but in this code by default nothing is there in “File of type” and in the list it show *.java, *.class, All File respectively.But I want to show *.java name as a default name in “File of type”.
When I change this code and put fileChooser.setAcceptAllFileFilterUsed(true); above to fileChooser.addChoosableFileFilter(new MyFilter()); then All File show defaule name in “File of type”.
If you save a text file in text editor then there Text Documents(.txt) show as a default and in list Text Documents(.txt) and All File is there.
I want same thing in my application.
I am also attaching photo which give clear information what I want:
My application work like this:


But I want like this:

Customizing a JFileChooser might be worth looking at as it does exactly as you describe.
The official JFileChooser documentation also covers your problem.