B4X Store
jdashboardUI / Files & Compression / FileChooserUtils
Files & Compression

FileChooserUtils

Helpers for extension filters, merged filters and initial JavaFX FileChooser selection

Search all jdashboardUI docs
Source
jdashboardUI.b4xlib · Public API only
Store language: en

Files & Compression · jdashboardUI · B4J · StaticCode · JavaFX FileChooser

FileChooserUtils centralizes the repetitive JavaFX code needed to configure FileChooser.ExtensionFilter objects from normal B4J lists.

It is useful when an application repeatedly opens or saves documents, images, ZIP backups, project files or other known formats.

What it provides

  • Add one extension filter per description.
  • Add a merged "Supported files" style filter.
  • Optionally add an All Files filter.
  • Combine merged + individual + All Files filters.
  • Select the initial filter by its description.

Important rule: list sizes must match

Every Add method expects Descriptions.Size = Filters.Size.

Dim Descriptions As List
Descriptions.Initialize2(Array As String( _
    "ZIP archives", _
    "PDF documents", _
    "Text files"))

Dim Filters As List
Filters.Initialize2(Array As String( _
    "*.zip", _
    "*.pdf", _
    "*.txt"))

If the sizes are different, the current implementation logs Extension filters not added and returns without changing the FileChooser.

AddExtensionFilters

Adds only the individual filters.

Dim FC As FileChooser
FC.Initialize
FC.Title = "Open document"

Dim Descriptions As List
Descriptions.Initialize2(Array As String("PDF documents", "Text files"))

Dim Filters As List
Filters.Initialize2(Array As String("*.pdf", "*.txt"))

FileChooserUtils.AddExtensionFilters(FC, Descriptions, Filters)

Dim Selected As String = FC.ShowOpen(MainForm)
If Selected <> "" Then Log(Selected)

AddExtensionFilters2

Adds a merged filter first, then all individual filters.

FileChooserUtils.AddExtensionFilters2( _
    FC, Descriptions, Filters, "Supported files")

For example, *.pdf, *.txt and *.zip can appear together under Supported files while still remaining available as individual filters.

AddExtensionFilters3

Adds the individual filters and optionally JavaFX's All Files (*.*) entry.

FileChooserUtils.AddExtensionFilters3( _
    FC, Descriptions, Filters, True)

Use False when you do not want unrestricted selection.

AddExtensionFilters4

This is the complete helper used internally by the other Add methods.

FileChooserUtils.AddExtensionFilters4( _
    FC, _
    Descriptions, _
    Filters, _
    True, _
    "Supported files", _
    True)
Parameter Meaning
FC Target FileChooser
Descriptions User-visible filter names
Filters JavaFX wildcard patterns such as *.zip
Merge Adds one combined filter when True
MergeName Description of the combined filter
AllFiles Adds All Files (*.*) when True

Select the default filter

SetSelectedExtensionFilter selects the first existing filter whose description exactly matches the supplied text.

FileChooserUtils.SetSelectedExtensionFilter(FC, "ZIP archives")

For a merged filter:

FileChooserUtils.SetSelectedExtensionFilter(FC, "Supported files")

For the unrestricted filter:

FileChooserUtils.SetSelectedExtensionFilter(FC, "All Files")

The comparison is against the description, not the extension pattern.

Complete example: choose a backup destination

Private Sub ChooseBackupFile As String
    Dim FC As FileChooser
    FC.Initialize
    FC.Title = "Save backup"

    Dim Descriptions As List
    Descriptions.Initialize2(Array As String("ZIP archives"))

    Dim Filters As List
    Filters.Initialize2(Array As String("*.zip"))

    FileChooserUtils.AddExtensionFilters3( _
        FC, Descriptions, Filters, True)

    FileChooserUtils.SetSelectedExtensionFilter( _
        FC, "ZIP archives")

    Return FC.ShowSave(MainForm)
End Sub

Private Sub CreateBackup
    Dim Target As String = ChooseBackupFile
    If Target = "" Then Return

    ' FileChooser filters do not automatically append a suffix.
    If Target.ToLowerCase.EndsWith(".zip") = False Then
        Target = Target & ".zip"
    End If

    Dim Zip As ZipFile
    Zip.Initialize(Target)
    Zip.SmartCompression = True

    If Zip.AddFolderSmart(File.Combine(File.DirApp, "data")) = False Then
        Log(Zip.LastError)
        Return
    End If

    If Zip.ValidateArchive Then
        Log("Backup created: " & Target)
    Else
        Log("Invalid backup: " & Zip.LastError)
    End If
End Sub

This combines FileChooserUtils for UX with ZipFile for the archive itself.

File extension behavior

The helper controls what the chooser displays. It does not automatically append a missing suffix to a path returned by ShowSave.

If Path.ToLowerCase.EndsWith(".zip") = False Then
    Path = Path & ".zip"
End If
  • FileUtils — recursive copy/delete, rename, subfolder discovery and timestamps.
  • ZipFile + ZipFileHeader — ZIP creation, encryption, reading, validation and inspection.

Best practices

  • Keep Descriptions and Filters the same size.
  • Use JavaFX wildcard patterns such as *.zip, *.pdf or *.txt.
  • Use a clear MergeName.
  • Call SetSelectedExtensionFilter after adding the filters.
  • Remember that save filters do not guarantee or append the filename extension.