JDK-4760449 : JFileChooser.getSelectedFile()fails to return file from file name typed by user
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2002-10-09
  • Updated: 2002-10-18
  • Resolved: 2002-10-18
Related Reports
Duplicate :  
Relates :  
Description

Name: sv35042			Date: 10/09/2002


FULL PRODUCT VERSION :
E:\Java>java -version
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

FULL OPERATING SYSTEM VERSION :
Windows NT 4.0, SP4

A DESCRIPTION OF THE PROBLEM :
The documentation of method JFileChooser.getSelectedFile()
says:
"Returns the selected file. This can be set either by the
programmer via setFIle or by a user action, such as either
typing the filename into the UI or selecting the file from a
list in the UI."

The behaviour of getSelectedFile does not fulfil this
contract:
It returns the file whose name has been typed in by the user
- correctly irrespective whether a file with this name
exists or not - only, after the Approve Button has been
pressed.
In any other case it will return the file that has been
selected from the UI list prior to typing in a file name, or
it will return null.

You can verify this as follows:
Install an extended JButton on the chooser as an accessory,
and in let this button?s ActionListener just call the
chooser?s getSelectedFile method.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Install an extended JButton on the chooser as an
accessory, and in let this button?s ActionListener just call
the chooser?s getSelectedFile method and print
getSelectedFile().getName() to the log, or the message "The
file is null".
2. Open the chooser Dialog - it has the accessory button on
the WEST -, type in a file name, and press the accessory
button. It will return null.
3. In the open chooser Dialog, select a file by clicking
into the file list, then overwrite the file name by typing,
then press the accessory button. It will return the name of
the clicked file, not the typed name.

This bug can be reproduced always.

---------- BEGIN SOURCE ----------
// This is the Demo, using JFileChooser and the accessory below
// _______________________________________________________________________
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;

public class MyChooserDemo extends JFrame {
    static private String newline = "\n";

    public MyChooserDemo() {
        super("MyChooserDemo");

        //Create the log first, because the action listener
        //needs to refer to it.
        final JTextArea log = new JTextArea(5,20);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(false);
        JScrollPane logScrollPane = new JScrollPane(log);

        JButton sendButton = new JButton("Open...");
        sendButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser();
                fc.addChoosableFileFilter(new ImageFilter());
                fc.setFileView(new ImageFileView());
                fc.setAccessory(new WildcardButton(fc));

                int returnVal = fc.showDialog(MyChooserDemo.this,
                                              "Open");

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    log.append("Opening file: " + file.getName()
                               + "." + newline);
                } else {
                    log.append("Open cancelled by user." + newline);
                }
            }
        });

        Container contentPane = getContentPane();
        contentPane.add(sendButton, BorderLayout.NORTH);
        contentPane.add(logScrollPane, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        JFrame frame = new MyChooserDemo();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        frame.pack();
        frame.setVisible(true);
    }
}
//__________________________________________________________________________
// This is the accessory
// __________________________________________________________________________
import javax.swing.*;
import java.beans.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

public final class WildcardButton extends JButton	{

	JFileChooser chooser;
	
	public WildcardButton(JFileChooser fc)	{
		super("WildcardFilter");
		this.chooser = fc;
		this.setEnabled(true);
		this.addActionListener(new ButtonListener());
	}
	
	class ButtonListener implements ActionListener	{
		public void actionPerformed(ActionEvent e)	{
			File f = chooser.getSelectedFile();
			if ( f != null )	{
				String p = f.getName();
				Out.println(p);
			} else {
				Out.println("There is no file selected");
			}
		}
	}
	
}

---------- END SOURCE ----------
(Review ID: 145395) 
======================================================================

Comments
EVALUATION This was mistakenly closed as duplicate of 4233870. It is in fact a duplicate of 4528663 instead. ###@###.### 2004-05-05
05-05-2004