Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to listen to the changes that occur when using the JFileChooser, see this snipet of code:</p> <pre><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(); } } }) ; </code></pre> <p>All you need to do inside the first condition is set the value of your textfield to match the new selected filename. See this example:</p> <pre><code>yourTextfield.setText(chooser.getSelectedFile().getName()); </code></pre> <p>Or just</p> <pre><code>yourTextfield.setText(curFile.getName()); </code></pre> <p>It is the method getName() from class File that will give you what you need. Help your self from de <a href="http://docs.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html" rel="nofollow noreferrer">API docs</a> to see what each method does.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload