• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

File Chooser  RSS feed

 
Ranch Hand
Posts: 70
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to allow selecting one specific file name Sample.txt

In line 5 I want to have something like "Source Template", "Sample.txt" and only those text files will be highlighted and selectable.
I can't figure the right way to do this.

Appreciate any help
Thanks Roger

 
Marshal
Posts: 6265
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Roger Wells
Ranch Hand
Posts: 70
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Is there a way to find just Sample.txt ?

Thanks Roger

 
Liutauras Vilda
Marshal
Posts: 6265
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roger Wells wrote: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.

Is there a way to find just Sample.txt ?


The correct one is only one you said - "Sample.txt".

If your goal is to open the file X, and that X you know, and user needs to select that X and nothing else - why do you want to involve that selection process, I don't get that part.

Maybe somebody else understands what you want as I don't - sorry. I don't get maybe because of tired, been hard day.
 
Rancher
Posts: 3352
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The file may be in any directory on the system.


Are you suggesting the program should search for the one file: "Sample.txt" through all the directories on the system?
If more than one is found, then present a choice to the user.
 
Roger Wells
Ranch Hand
Posts: 70
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want the user to be able to brouse to a directory they know where the right Sample.txt file is located. There can be several Sample.txt files each in a seperate directory

By only allowing them to see the Sample.txt it will reduce the possibility of error when there can be many other text files in the same directory.

Roger
 
Bartender
Posts: 1986
59
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really really hate giving this solution... If I was a user, I would protest against such application:

 
salvin francis
Bartender
Posts: 1986
59
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Sheriff
Posts: 23877
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like you want a directory chooser, not a file chooser. No? To make a JFileChooser display only directories you need a line of code like this:

 
Norm Radder
Rancher
Posts: 3352
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I were a user of this program I'd be annoyed if I had to search through folders for a file named "Sample.txt".  I figure searching for files is a program's job.
 
Liutauras Vilda
Marshal
Posts: 6265
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

i.e.:

Templates:
v1  c:\documents\templates\v1\Sample.txt
v2  c:\documents\templates\v2\Sample.txt
...
[select] [cancel]

The appended version number at the beginning could be extracted from the Path object.
 
salvin francis
Bartender
Posts: 1986
59
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Roger Wells
Ranch Hand
Posts: 70
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions

I have decided to not implement this but to just select based on the file extension at this time.

Roger
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!