Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX FileChooser: how to set file filters?

I want to set file filters in a JavaFX FileChooser but I could not find a way to do it.

My code:

  FileChooser fc = new FileChooser();   fc.setTitle("My File Chooser");   File f = fc.showOpenDialog(primaryStage);   System.out.println(f); 
like image 705
ceklock Avatar asked Nov 29 '12 20:11

ceklock


People also ask

Which FileChooser JavaFX stage FileChooser method returns the file the user selected?

The File returned by the showOpenDialog() method is the file the user selected in the FileChooser . The stage parameter is the JavaFX Stage that should "own" the FileChooser dialog.

What is file chooser in JavaFX?

FileChooser class is a part of JavaFX. It is used to invoke file open dialogs for selecting a single file (showOpenDialog), file open dialogs for selecting multiple files (showOpenMultipleDialog) and file save dialogs (showSaveDialog). FileChooser class inherits Object class.

What is an extension filter?

The Extension Filtering feature is used to determine whether the configuration should check certain file types or if it should ignore certain file types.


2 Answers

You could do:

FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt"); chooser.getExtensionFilters().add(extFilter); 

Here is a simple example:

public class ExtensionFilterExample extends Application {      public static void main(String[] args) {         launch(args);     }      @Override     public void start(final Stage primaryStage) {         primaryStage.setTitle("Extension Filter Example");          final Label fileLabel = new Label();          Button btn = new Button("Open FileChooser");         btn.setOnAction(new EventHandler<ActionEvent>() {              @Override             public void handle(ActionEvent event) {                 FileChooser fileChooser = new FileChooser();                  // Set extension filter                 FileChooser.ExtensionFilter extFilter =                          new FileChooser.ExtensionFilter("TEXT files (*.txt)", "*.txt");                 fileChooser.getExtensionFilters().add(extFilter);                  // Show open file dialog                 File file = fileChooser.showOpenDialog(primaryStage);                 if (file != null) {                     fileLabel.setText(file.getPath());                 }             }         });          VBox vBox = new VBox(30);         vBox.getChildren().addAll(fileLabel, btn);         vBox.setAlignment(Pos.BASELINE_CENTER);          StackPane root = new StackPane();         root.getChildren().add(vBox);         primaryStage.setScene(new Scene(root, 300, 250));         primaryStage.show();     } } 
like image 171
Reimeus Avatar answered Oct 02 '22 10:10

Reimeus


Update for JavaFX plus multiple extensions filter:

FileChooser fc = new FileChooser();  FileChooser.ExtensionFilter fileExtensions =    new FileChooser.ExtensionFilter(     "Web pages", "*.tpl", "*.html", "*.htm");  fc.getExtensionFilters().add(fileExtensions); 
like image 44
Zon Avatar answered Oct 02 '22 12:10

Zon