0

Is there a way to access the TextField of a JFileChooser to add an event listener?

The idea would be to add an event listener to the text field that checks the path for correctness. Note that a file filter is not an option, since the user must be able to select folders. Futhermore, I can't filter folders because the user must be able to navigate the folder hierarchy. Activating and deactivating the open button as discussed here is also not really a valid solution since the selection is validated only when the user clicks a folder. However, the user may also enter the path directly into the text field.

Community
  • 1
  • 1
dastrobu
  • 1,600
  • 1
  • 18
  • 32
  • Why isn't the question you link to a valid approach (Or something like [this](http://stackoverflow.com/questions/4232275/altering-jfilechooser-behaviour-preventing-choose-on-enter-in-file-path-jtex) ) ? Sure you don't need to activate/deactivate the buttons, but you could do whatever you need to do instead. When do you want to validate the text field, e.g. at every character the user types ? And what do you want to do about it ? – nos Dec 02 '14 at 09:08
  • Validating every character the user types is exactly the problem. Therefore, I need to register an event listener that is called everytime the user types a character or more general, when the path in the text field changes. In the linked question, the state of the button is only updated when the user clicks on a different file/folder. – dastrobu Dec 02 '14 at 09:18
  • Why wouldn't a corresponding `FileFilter` work for you? – Christian Hujer Dec 02 '14 at 09:40
  • As stated in the question, because all folders must be visible so the user can navigate the folder hierarchy. However, only certain Folders must be selectable. Additionally, the user could still enter an invalid path manually. – dastrobu Dec 02 '14 at 09:47
  • Interestingly, this resembles the default behavior on Mac OS X; files rejected by the `FileFilter` are shown as disabled. You may have to look at the `FileChooserUI` for your chosen L&F. – trashgod Dec 02 '14 at 12:26

0 Answers0