Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8638345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:42:22+00:00 2026-06-12T10:42:22+00:00

For a simple Swing application for merging PDFs with Apache PDFBox I’m using a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:42:23+00:00Added an answer on June 12, 2026 at 10:42 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Swing Java application that performs searches, and the results are
I am working on a simple java swing based application. How would I get
I'm writing a simple Windows forms application to get me into the swing of
I'm writing a simple loan calculator with gui using swing. I used DecimalFormat to
I'm writting simple Swing application. I can't uderstand why column name isn't displayed? Here
I'm debeloping a Java Swing application, which persists the information through Hibernate, currently using
I made a simple Swing application. But the rendering behaves buggy. Have I done
i am trying to make a 3rd party simple standalone/swing application that uses hibernate
I created a simple GUI Application with Swing in Scala, and the Run as
For a very simple application, package mypackage; import javax.swing.*; public class Main { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.