Java: Show "*.java" in "File of type" as default in fileChooser

10,685

Solution 1

Customizing a JFileChooser might be worth looking at as it does exactly as you describe.

The official JFileChooser documentation also covers your problem.

Solution 2

Try in this way :

JFileChooser fS = new JFileChooser();
fS.setAcceptAllFileFilterUsed(false);
fS.setMultiSelectionEnabled(false);
fS.setFileFilter(new FileNameExtensionFilter(wordExtDesc, ".class"));
fS.setFileFilter(new FileNameExtensionFilter(excelExtDesc, ".java"));

Inverting the order of the filter set, the last filter added should appear as the default ...

Solution 3

If you call this after adding all your filters and setAcceptAllFileFilterUsed, then All files is selected by default:

fileChooser.setFileFilter(fileChooser.getChoosableFileFilters()[2]);

Of course choose the correct index to have the one you want selected by default

Solution 4

My way:

//set default dir to user home
JFileChooser fileChooser = new JFileChooser(System.getProperty("user.home"));
//items in filter dropdown box
FileFilter excelType = new FileNameExtensionFilter("Excel spreadsheet (.xls)", "xls");
FileFilter wordType = new FileNameExtensionFilter("Word document (.doc)", "doc");
fileChooser.addChoosableFileFilter(excelType);
fileChooser.addChoosableFileFilter(wordType);
//default (initial) filter predefined in dropdown box
fileChooser.setFileFilter(excelType);
//initial file name
fileChooser.setSelectedFile(new File("report"));
//clear "All files" from dropdown filter box
fileChooser.setAcceptAllFileFilterUsed(false);
//open filechooser dialog
int response = fileChooser.showSaveDialog(null);
if(response == JFileChooser.APPROVE_OPTION)
{
    //do somethink...
}
Share:
10,685
Vinit ...
Author by

Vinit ...

Development Engineer

Updated on June 05, 2022

Comments

  • Vinit ...
    Vinit ... almost 2 years

    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:

    enter image description here

    enter image description here

    But I want like this:

    enter image description here