You need to listen to the changes that occur when using JFileChooser, see this snippet of code:
JFileChooser chooser = new JFileChooser(); // Add listener on chooser to detect changes to selected file chooser.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY .equals(evt.getPropertyName())) { JFileChooser chooser = (JFileChooser)evt.getSource(); File oldFile = (File)evt.getOldValue(); File newFile = (File)evt.getNewValue(); // The selected file should always be the same as newFile File curFile = chooser.getSelectedFile(); } else if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals( evt.getPropertyName())) { JFileChooser chooser = (JFileChooser)evt.getSource(); File[] oldFiles = (File[])evt.getOldValue(); File[] newFiles = (File[])evt.getNewValue(); // Get list of selected files // The selected files should always be the same as newFiles File[] files = chooser.getSelectedFiles(); } } }) ;
All that you need to do in the first condition sets the value of your text field in accordance with the newly selected file name. See this example:
yourTextfield.setText(chooser.getSelectedFile().getName());
Or simply
yourTextfield.setText(curFile.getName());
This is the getName () method from the File class, which will give you what you need. Help yourself from the API docs to find out what each method does.
source share