JFileChooser.getSelectedFile() return null when implement custom action button
<p>I have a <code>JFrame</code> with <code>JFileChooser</code>. There is need to have a custom import button instead of default file chooser action button.</p>

<p>If i use custom action button, <code>JFileChooser.getSelectedFile()</code> return null if i enter value in File Name textbox. Whereas if i click the file and click custom import, i can get the file which i chosen.</p>

<p>Here i included the sample code to reproduce this</p>

<pre><code>import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FileChooserDemo extends JPanel
                             implements ActionListener {
    private static final long serialVersionUID = 1L;
    JFileChooser importFileChooser;
    JFrame frame;

    private void createAndShowGUI() {
        //Create and set up the window.
        frame = new JFrame("FileChooserDemo");

        JPanel inputJobDetailsPanel = new JPanel(new BorderLayout(0,5));

        importFileChooser = new JFileChooser();
        importFileChooser.setControlButtonsAreShown(false);
        importFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        importFileChooser.setMultiSelectionEnabled(false);
        inputJobDetailsPanel.add(importFileChooser, BorderLayout.CENTER);

        GridBagLayout importButtonPanelLayout = new GridBagLayout();
        importButtonPanelLayout.columnWidths = new int[] {150};
        importButtonPanelLayout.rowHeights = new int[] {30};

        JPanel importButtonPanel = new JPanel();
        importButtonPanel.setLayout(importButtonPanelLayout);

        JButton importButton = new JButton("Custom Import");
        importButton.setActionCommand("import");
        importButton.addActionListener(this);
        importButtonPanel.add(importButton, new GridBagConstraints());

        JButton OtherButton = new JButton("Other Action");
        OtherButton.setActionCommand("otherImport");
        OtherButton.addActionListener(this);
        importButtonPanel.add(OtherButton, new GridBagConstraints());

        inputJobDetailsPanel.add(importButtonPanel, BorderLayout.PAGE_END);

        frame.add(inputJobDetailsPanel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new FileChooserDemo().createAndShowGUI();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if(command.equals("import")) {
            if(importFileChooser.getSelectedFile() == null) {
                JOptionPane.showMessageDialog(frame, "You entered file name but getSelectedFile() return null");
            }else {
                JOptionPane.showMessageDialog(frame, "Chosen File Name: " + importFileChooser.getSelectedFile().getName());
            }
        }else {
            JOptionPane.showMessageDialog(frame, "You clicked other action");
        }

    }
}
</code></pre>

<p>O/P:</p>

<p><a href="https://i.stack.imgur.com/MEFDx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MEFDx.png" alt="enter image description here"></a></p>

<p>Steps to reproduce:</p>

<ol>
<li>Run the application</li>
<li>Enter a valid file name in "File Name" text box</li>
<li>Click Custom Import</li>
<li>Now you can see "You entered file name but getSelectedFile() return null"</li>
</ol>

<p>Note: If i enabled default action button by using  <code>importFileChooser.setControlButtonsAreShown(true);</code></p>

<p>I can get the getSelectedFile() even i entered in textbox without clicking on the file. </p>

<p>Actually i am trying to write an automation script, so i can enter only file path via "File Name" Text box.</p>

<p>Any idea to get the files by getSelectedFile() without clicking on the file??</p>