I have a jfilechooser set to DIRECTORIES_ONLY mode.
What I do not like about my current jfilechooser is selecting folders in the directories_only mode shows the full absolute path in the folder name.
Is there any way to override this behavior to just show the folder name only like in the case of a file?
My jfilechooser is meant to
specify a name for the folder that is about to be created to save contents to
OR
if a folder is selected, overwrite that folder
I am not coding in java but in kawa (jvm scheme) so I can only give the solution as java-like as I can.
Basically I added a PropertyChangeListener to my JFileChooser
Override propertyChange method to do the following
I listen to the property change SELECTED_FILE_CHANGED_PROPERTY and set the file name display manually to the FileChooserUI which is part of JFileChooser.
Note that all these is just for aesthetics, the folder chosen is not in anyway changed. It is just that the filename display would not should the full path this way but only the name of the folder you just selected.
Here is my attempt to write java code without testing. I'll test this again when I have more time.
JFileChooser folder_chooser = new JFileChooser();
folder_chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
PropertyChangeListener folder_name_changer = new PropertyChangeListener () {
public void propertyChange(PropertyChangeEvent e) {
String property_name = e.getPropertyName();
JFileChooser chooser = e.getSource();
if (property_name.equals(JFileChooserSELECTED_FILE_CHANGED_PROPERTY) {
File selected_file = chooser.getSelectedFile();
FileChooserUI chooser_ui = chooser.getUI();
// BasicFileChooserUI is the subclass that implements a setFileName method
if ( selected_file != null && (chooser_ui instanceof BasicFileChooserUI)) {
chooser_ui.setFileName( selected_file.getFileName() );
}
}
}
};
folder_chooser.addPropertyChangeListener( folder_name_changer );
I had the same issue. I removed the bit about setting the mode to DIRECTORIES_ONLY, and instead used a File filter so that only directories would show up in the browse view:
chooser.setFileFilter(new FileFilter() {
def accept(f: File) = f.isDirectory
})
(Sorry, that's Scala, but it should be clear enough).
This won't work particularly well for your second use case, however, where you want the user to be able to choose a directory to be overwritten.