Maglioni Lorenzo
Maglioni Lorenzo

Reputation: 378

Javafx filechooser name filter

i'd like to know if there's a way of filtering the names of files to make them selectable in the dialog to select files, for instance all files that starts for "A" and are in txt format, i searched a bit and i found only tips topics about the extension with the Extension filter, that's fine but i'd like to select just some file in a format.

Upvotes: 4

Views: 2232

Answers (4)

Ivan Shuba
Ivan Shuba

Reputation: 1

You can use wildcards directly in the ExtensionFilter. For example, if you want to select files starting with 'A' character, or ending with 'B' character, or having 'C' character in the middle, AND having txt extension then you can do this as follows:

String description = "Text files starting with 'A' ending with 'B' or having 'C' in the middle of name";
String[] extensions = new String[] { "A*.txt", "*B.txt", "*C*.txt" };
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extensionFilter = new FileChooser.ExtensionFilter(description, extensions);
fileChooser.getExtensionFilters().addAll(extensionFilter);

Upvotes: 0

Rahul Singh
Rahul Singh

Reputation: 19640

        FileChooser chooser = new FileChooser();
        chooser.getExtensionFilters().addAll(new ExtensionFilter("Excel Files", "*.xls"));

you can use add or addAll depending upon how many filters you want to add.

Upvotes: 0

Coder ACJHP
Coder ACJHP

Reputation: 2224

Of course , you can get some idea from this example; in Java Swing( I'm not sure how in JavaFX) you can filter files by name or extension like :

FileChooser fileChooser = new FileChooser();
FileFilter filter = new FileNameExtensionFilter("MP3 File","mp3");
fileChooser.setFileFilter(filter)`

Upvotes: 0

Joseph Earl
Joseph Earl

Reputation: 23432

In JavaFX you can filter for particular file types by adding ExtensionFilters to the list of filters returned by getExtensionFilters, like so:

FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.txt"));

The JavaFX file chooser does not support filtering by file name, only by extension. This is because most platforms don't support this functionality natively in their file choosers.

Upvotes: 5

Related Questions