B4J Question B4J OpenFile Dialog...

GMan

Well-Known Member
Licensed User
Longtime User
Is there some kind of "B4J OpenFile Dialog" or similar ?
I want to load an image in a ImageView--
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
B4X:
Private Sub openFileDialog(frame As JavaObject) As String   
    Dim fileChooser As JavaObject
    fileChooser.InitializeNewInstance("javax.swing.JFileChooser", Array(Null))
     Dim result As Int = fileChooser.RunMethod("showOpenDialog", Array(jframe))
    If result = fileChooser.GetField("APPROVE_OPTION") Then
        Dim selectedFile As JavaObject = fileChooser.RunMethod("getSelectedFile", Null)
        Dim filePath As String = selectedFile.RunMethod("getPath", Null)
        Log("Selected file Path: " & filePath)
        Dim FileName As String = fileChooser.RunMethodJO("getSelectedFile",Null).RunMethod("getName",Null)
        Log($"Selected File name:${FileName}"$)
        ' Process the selected file here
    Else
        Log("No file selected.")
    End If

End Sub
'to call it:
    Dim jframe As JavaObject
    jframe.InitializeNewInstance("javax.swing.JFrame",Array(""))
    openFileDialog(jframe)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
B4X:
    Dim fchoose As FileChooser
    fchoose.Initialize
    fchoose.InitialDirectory = "."
    fchoose.InitialFileName = "*.java"
    Dim fname As String = fchoose.ShowOpen(MainForm)
If fname = "" then 
'no file chosen 
Else 
'it contains selected file name and path.
End If
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
B4X:
    Dim fchoose As FileChooser
    fchoose.Initialize
    fchoose.InitialDirectory = "."
    fchoose.InitialFileName = "*.java"
    Dim fname As String = fchoose.ShowOpen(MainForm)
If fname = "" then
'no file chosen
Else
'it contains selected file name and path.
End If
I got an error using this:

B4X:
    Dim fchoose As FileChooser
    fchoose.Initialize
    fchoose.InitialDirectory = "."
    fchoose.InitialFileName = "*.jpg"
    Dim fname As String = fchoose.ShowOpen(Root)
    If fname = "" Then
        'no file chosen
    Else
        'it contains selected file name and path.
    End If

I have to use "Root" instead of "MainForm"

The error log entry:
B4X:
java.lang.ClassCastException: class javafx.scene.layout.AnchorPane cannot be cast to class anywheresoftware.b4j.objects.Form (javafx.scene.layout.AnchorPane is in module javafx.graphics of loader 'app'; anywheresoftware.b4j.objects.Form is in unnamed module of loader 'app')

And this one:
B4X:
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
B4X:
Private Sub openFileDialog(frame As JavaObject) As String  
    Dim fileChooser As JavaObject
    fileChooser.InitializeNewInstance("javax.swing.JFileChooser", Array(Null))
     Dim result As Int = fileChooser.RunMethod("showOpenDialog", Array(jframe))
    If result = fileChooser.GetField("APPROVE_OPTION") Then
        Dim selectedFile As JavaObject = fileChooser.RunMethod("getSelectedFile", Null)
        Dim filePath As String = selectedFile.RunMethod("getPath", Null)
        Log("Selected file Path: " & filePath)
        Dim FileName As String = fileChooser.RunMethodJO("getSelectedFile",Null).RunMethod("getName",Null)
        Log($"Selected File name:${FileName}"$)
        ' Process the selected file here
    Else
        Log("No file selected.")
    End If

End Sub
'to call it:
    Dim jframe As JavaObject
    jframe.InitializeNewInstance("javax.swing.JFrame",Array(""))
    openFileDialog(jframe)
In the line
B4X:
Dim result As Int = fileChooser.RunMethod("showOpenDialog", Array(jframe))

the last jframe is read - what lib am i missing ?
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
In the line
B4X:
Dim result As Int = fileChooser.RunMethod("showOpenDialog", Array(jframe))

the last jframe is read - what lib am i missing ?
Use JavaObject library and include jframe in Process_Global:
B4X:
Sub Process_Global

   Private jframe As JavaObject



End Sub
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    Button1.Text="Open File Dialog"
      
    jframe.InitializeNewInstance("javax.swing.JFrame",Array(""))
   ' jframe.RunMethod("setSize", Array(30, 20)) 'optional
End Sub
Private Sub openFileDialog(frame As JavaObject) As String
    Dim fileChooser As JavaObject
    fileChooser.InitializeNewInstance("javax.swing.JFileChooser", Array(Null))
     Dim result As Int = fileChooser.RunMethod("showOpenDialog", Array(jframe))
    If result = fileChooser.GetField("APPROVE_OPTION") Then
        Dim selectedFile As JavaObject = fileChooser.RunMethod("getSelectedFile", Null)
        Dim filePath As String = selectedFile.RunMethod("getPath", Null)
        Log("Selected file Path: " & filePath)
        Dim FileName As String = fileChooser.RunMethodJO("getSelectedFile",Null).RunMethod("getName",Null)
        Log($"Selected File name:${FileName}"$)
        ' Process the selected file here
    Else
        Log("No file selected.")
    End If

End Sub

'to call it:
  
Private Sub Button1_Click
    openFileDialog(jframe)
End Sub
1722166590492.png
 
Upvote 0
Top