Roger Wells wrote:In line 5 I want to have something like "Source Template", "Sample.txt" and only those text files will be highlighted and selectable.
In line 5 you are using FileNameExtensionFilter. "Sample.txt" isn't an extension. Extension is what is after the last . (dot). In your case it is "txt".
Anyway, you can have at most one filename Sample.txt in the same file system directory. Why do you want to filter Sample.txt only? If you want user to select such file, just assume user selected that if that's what you want.
Not quite clear to me actually, what exactly you want. Concrete examples maybe with 3 random filenames?
posted 2 years ago
The file may be in any directory on the system. There maybe be many text files in the directory. So wherever the file is I want to make sure they get the correct one.
Ideally, you need to build up a very strong use case to allow your application to do something like that.
Why would you restrict your users from selecting any file as opposed to a file matching a particular extension ?
Isn' t it possible for your application to actually read the file to validate it's contents ? Your application should be independent of the file name user gives.
What if the user downloads a troll gif image and simply renames it to "sample.txt", will your application accept it ?
And my final question
Have you used any application which only allows you to select 'x' file and not 'y' as per your usecase ?
While Salvin's solution seems closest (at least for now) to what OP described, but I tend to agree with Norm.
@OP. Imagine you go through thousand of directories and all of them look empty except one. Why such efforts to put on user?
If you could narrow down the area enough where such templates possibly can be, your program probably would be much more useful if would find all existing templates and would give an ability for user to choose a needed one at a given point in time.
If you are very keen on allowing the user to select only sample.txt. From UI standpoint, here would be my solution:
Show a wizard with the option to select root parent directory as the first screen. This would be the parent directory to search for sample.txt.
When user clicks next, show an indeterminate progress bar (see here) and recursively search the directory and its children for sample.txt.
Once you get all the files, show a table with the file paths and allow the user to select the file(s).