Clear JFileChooser selection after adding files to a JList
Asked Answered
P

3

10

For a simple Swing application for merging PDFs with Apache PDFBox I'm using a JFileChooser to select one or multiple PDF files and add it/them to a JList. No problems so far.

What bothers me is that the previous selection persists in the JFileChooser when I click the button to add another file/files again, I do not want this, the selection should initially be empty.

I tried this but it neither works nor throws an exception:

    pdfFileChooser.setSelectedFile(null);

Here is the relevant code:

    pdfFileChooser.setAcceptAllFileFilterUsed(false);
    pdfFileChooser.setMultiSelectionEnabled(true);
    pdfFileChooser.setFileFilter(new FileFilter() {

       @Override
       public boolean accept(File arg0) {
          return arg0.getName().endsWith(".pdf");
       }
       @Override
       public String getDescription() {
          return "*.pdf";
       }
    } );

    JButton btnAddFile = new JButton("Add file");
    btnAddFile.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent arg0) {
          if(pdfFileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
             addFileToList(pdfFileChooser.getSelectedFiles());
             pdfFileChooser.setSelectedFile(null);
          }
       }
    });

    private void addFileToList(File[] filesToAdd) {
       if((filesToAdd != null) && (filesToAdd.length > 0)) {
          DefaultListModel model = (DefaultListModel)listFiles.getModel();
          for(File file : filesToAdd) {
             if(!model.contains(file)) {
                model.addElement(file);                 
             }
          }
       }
    }

How can I remove the selection from the JFileChooser so no file/files is/are initially selected?

Perry answered 4/10, 2012 at 22:19 Comment(0)
L
14

This is, IMHO, a bug.

(As has been kindly pointed out, "bug" might be to strong a word as the API does not state what would happen if you passed null to the selectedFile method. Instead, it's probably more reasonable to suggest that it is a missing feature)

Try something like this...

JFileChooser fc = new JFileChooser();
fc.showOpenDialog(null);
File selected = fc.getSelectedFile();
System.out.println("You selected " + selected);

File currentDirectory = fc.getCurrentDirectory();
// Hack alert
fc.setSelectedFile(new File(""));
fc.setCurrentDirectory(currentDirectory);

fc.showOpenDialog(null);
selected = fc.getSelectedFile();

System.out.println("You selected " + selected);

Basically, the change of the selected file seems to be UI dependent and relies on the SELECTED_FILE_CHANGED_PROPERTY property change event. What seems to happen is that it's ignoring a null reference when it comes to changing (in particular) the selected file text field.

I tested this using Metal and Windows look and feel

Lavernelaverock answered 4/10, 2012 at 22:45 Comment(6)
+1 The bug may be that there's no explicit way to clear the current selections. One could hope that passing null would do it, but the API clearly doesn't specify that behavior. There's no mention of clearing the current selections anywhere that I can find.Perseid
@Jim agreeded, there is nothing in the docs that suggest what would happen if you passed a null to the setSelectedFile methodLavernelaverock
Nice, this works, thank you :) It is sufficient to do this: File currentDirectory = fc.getCurrentDirectory(); // Hack alert fc.setSelectedFile(new File("")); fc.setCurrentDirectory(currentDirectory);Perry
fc.setSelectedFile(new File("")); was all I needed to make it work. Thanks!Rotor
It doesn't seem to work for me on MacOSX (El Capitan Java 8)Gamester
@JohnTangBoyland I'd verify that it work with Java 8 on Windows, using Metal L&F, as different L&F/OS's may behave differentlyLavernelaverock
M
1

This works:

 fileChooser.setSelectedFile(new File(""));
 fileChooser.setSelectedFiles(new File[]{new File("")});

But if you want a faster alternative, in case you have selected more that 10K files (the previous code would take a lot of time).

 fileChooser.setSelectedFile(new File(""));
 ((FilePane) fileChooser.getComponents()[2]).clearSelection();
Mccomb answered 10/11, 2012 at 18:11 Comment(0)
B
0

I would just create a new JFileChooser instance and pass the last used directory to it. Last used directory defaults to home dir, and when you select a valid file you store the directory of that file and use it the next time you open a new JFileChooser.

Breadbasket answered 21/1, 2015 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.