Italian [B4J] FileChooser - ShowSave - più opzioni per setExtensionFilter

Elric

Well-Known Member
Licensed User
Ciao,

vorrei avere la finestra che permetta di salvare il file in diverse estensioni.

Questa:
B4X:
    Private fc As FileChooser
    fc.Initialize
 
    fc.setExtensionFilter("Data", Array As String("*.xls", "*.db", "*.xlsx", "*.csv"))
 
    fc.InitialDirectory = strDirPath
    FileChosen = fc.ShowSave(B4XPages.GetNativeParent(Me))
non è ovviamente la soluzione ottimale, perché non mi permette di scegliere in che estensione salvare il file.

È possibile?
Se si, come correggere/riscrivere il codice?

Grazie!
 
Solution
B4X:
'   B4J
    'fc = FileChooser
    (Me).As(JavaObject).RunMethod("setFilters",Array(fc,"Headers C","*.h"))
    (Me).As(JavaObject).RunMethod("setFilters",Array(fc,"Headers C++","*.hpp"))
 
#if java
import javafx.stage.FileChooser;

public static void setFilters(javafx.stage.FileChooser fc,String txt,String flt){
    fc.getExtensionFilters().add(new FileChooser.ExtensionFilter(txt,flt));
}
#End If

or without inline java
B4X:
' fc = FileChooser
    Dim filter As JavaObject
    filter.InitializeNewInstance("javafx.stage.FileChooser.ExtensionFilter",Array("test",Array As String("*.tst")))
    fc.As(JavaObject).RunMethodJO("getExtensionFilters",Null).RunMethod("add",Array(filter))


or
B4X:
Sub setFileFilters(f As FileChooser,txt()...

Daestrum

Expert
Licensed User
Longtime User
B4X:
'   B4J
    'fc = FileChooser
    (Me).As(JavaObject).RunMethod("setFilters",Array(fc,"Headers C","*.h"))
    (Me).As(JavaObject).RunMethod("setFilters",Array(fc,"Headers C++","*.hpp"))
 
#if java
import javafx.stage.FileChooser;

public static void setFilters(javafx.stage.FileChooser fc,String txt,String flt){
    fc.getExtensionFilters().add(new FileChooser.ExtensionFilter(txt,flt));
}
#End If

or without inline java
B4X:
' fc = FileChooser
    Dim filter As JavaObject
    filter.InitializeNewInstance("javafx.stage.FileChooser.ExtensionFilter",Array("test",Array As String("*.tst")))
    fc.As(JavaObject).RunMethodJO("getExtensionFilters",Null).RunMethod("add",Array(filter))


or
B4X:
Sub setFileFilters(f As FileChooser,txt() As String, ext() As String)
 if txt.length <> ext.length then Log("Error lists not same length")
    For a=0 To txt.Length - 1
        Dim filter As JavaObject
        filter.InitializeNewInstance("javafx.stage.FileChooser.ExtensionFilter",Array(txt(a),Array As String(ext(a))))
        f.As(JavaObject).RunMethodJO("getExtensionFilters",Null).RunMethod("add",Array(filter))
    Next
End Sub

...

    setFileFilters(fc,Array As String("Header C","Header C++"),Array As String("*.h","*.hpp"))

 
Last edited:
Upvote 1
Solution

Elric

Well-Known Member
Licensed User
Grazie Daestrum! (Thanks Daestrum!)

Non sono capace di far funzionare il primo esempio ma il secondo funziona creando più filtri:
(I'm not able to make working the first example, but the second works adding more filters: )
B4X:
    Dim filter1 As JavaObject
    filter1.InitializeNewInstance("javafx.stage.FileChooser.ExtensionFilter",Array("CVS",Array As String("*.csv")))
    fc.As(JavaObject).RunMethodJO("getExtensionFilters",Null).RunMethod("add",Array(filter1))
    Dim filter2 As JavaObject
    filter2.InitializeNewInstance("javafx.stage.FileChooser.ExtensionFilter",Array("Excel",Array As String("*.xls")))
    fc.As(JavaObject).RunMethodJO("getExtensionFilters",Null).RunMethod("add",Array(filter2))
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
look at the last example I added
 
Upvote 1
Cookies are required to use this site. You must accept them to continue using the site. Learn more…