B4J Question Force FileChooser to one directory and it's sub-folders

Daestrum

Expert
Licensed User
Longtime User
Just set the FileChooser InitialDirectory to the one you want

B4X:
Dim fc As FileChooser
...
fc.Initialize
fc.InitialDirectory = "C:/temp"
...
 
Upvote 0

hanyelmehy

Active Member
Licensed User
Longtime User
Just set the FileChooser InitialDirectory to the one you want

B4X:
Dim fc As FileChooser
...
fc.Initialize
fc.InitialDirectory = "C:/temp"
...
i already use InitialDirectory , i need to Force FileChooser to use only this directory
when using InitialDirectory client can select other Directory
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I did find this an interesting question.
Here's an example that shows how to get the information you want (didn't use dialogs just a plain listview) uses javaobject.
(Apologies in advance for the horrendous coding - perfect example of how my brain works - but hey it's fun to work out what does what)
B4X:
Sub Process_Globals
 Private fx As JFX
 Private MainForm As Form
 Dim lv As ListView
 Dim fsv As JavaObject
 Dim swingUtils As JavaObject
 Dim directory As String = ""
End Sub
Sub AppStart (Form1 As Form, Args() As String)
 MainForm = Form1
 MainForm.Show
 fsv.InitializeStatic("javax.swing.filechooser.FileSystemView")
 swingUtils.InitializeStatic("javafx.embed.swing.SwingFXUtils")
 directory = "c:/temp/"
 lv.Initialize("lv")
 For Each f As Object In File.ListFiles(directory)
  Dim rFile As JavaObject = new("java.io.File",Array(directory & f))
  If Not(rFile.RunMethod("isDirectory",Null)) Then
   Dim icon As Image = swingUtils.RunMethod("toFXImage",Array(fsv.RunMethodJO("getFileSystemView",Null).RunMethodjo("getSystemIcon",Array(rFile)).RunMethod("getImage",Null),Null))
   Dim p As Pane
   p.Initialize("")
   Dim img As ImageView
   img.Initialize("")
   Dim lab,lab1 As Label
   lab.Initialize("")
   lab1.Initialize("")
   img.SetImage(icon)
   lab.text = f
   lab.TooltipText=fsv.RunMethodJO("getFileSystemView",Null).RunMethod("getSystemDisplayName",Array(rFile))&CRLF& _
       fileSize(rFile.RunMethod("length",Null))
   lab1.Text ="["& fsv.RunMethodJO("getFileSystemView",Null).RunMethod("getSystemTypeDescription",Array(rFile))&"]"
   p.AddNode(img,0,0,20,20)
   p.AddNode(lab,25,0,200,20)
   p.AddNode(lab1,230,0,200,20)
   lv.Items.Add(p)
  End If
 Next
 MainForm.RootPane.AddNode(lv,10,10,500,500)
End Sub
Sub lv_SelectedIndexChanged(Index As Int)
 Log("You chose : " & asLabel(asPane(lv.Items.Get(Index)).GetNode(1)).Text)
End Sub
Sub new(className As String,args() As Object) As JavaObject
 Dim j As JavaObject
 Return j.InitializeNewInstance(className,args)
End Sub
Sub asLabel(l As Object) As Label
 Return l
End Sub
Sub asPane(p As Object) As Pane
 Return p
End Sub
Sub fileSize(s As Long) As String
 Dim t As Double
 Dim gb As Long = 1024*1024*1024
 Dim mb As Long = 1024*1024
 Dim kb As Long = 1024
 Select True
  Case s>gb
   t = s/gb
   Return NumberFormat(t,1,0)& "GB"
  Case s>mb
   t = s/mb
   Return NumberFormat(t,1,0)& " MB"
  Case s>kb
   t = s/kb
   Return NumberFormat(t,1,0)& " KB"
  Case Else
   Return s&" Bytes"
 End Select
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub
 

Attachments

  • filechoice.png
    filechoice.png
    44.6 KB · Views: 317
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I realized the version above doesn't allow you to navigate sub folders in the selected directory. This version does ( plus I cleaned up the code (a bit))
B4X:
Sub Process_Globals
 Private fx As JFX
 Private MainForm As Form
 Dim lv As ListView
 Dim fsv As JavaObject
 Dim swingUtils As JavaObject
 Dim directory As String = ""
 Dim depth As List
End Sub
Sub AppStart (Form1 As Form, Args() As String)
 MainForm = Form1
 MainForm.Show
 depth.Initialize
 fsv.InitializeStatic("javax.swing.filechooser.FileSystemView")
 swingUtils.InitializeStatic("javafx.embed.swing.SwingFXUtils")
 directory = "c:/temp"
 lv.Initialize("lv")
 MainForm.RootPane.AddNode(lv,10,10,500,500)
 If Not(directory.EndsWith("/")) Then directory=directory&"/"
 depth.Add(directory)
 buildFileList(directory)
End Sub
Sub buildFileList(root As String)
 lv.Items.Clear
 lv.Items.Add(buildPane(new("java.io.File",Array(root)),True))
 For Each f As Object In File.ListFiles(root)
  lv.Items.Add(buildPane(new("java.io.File",Array(root & f)),False))
 Next
End Sub
Sub lv_SelectedIndexChanged(Index As Int)
 If Index = -1 Then Return
 If Index = 0 And depth.Size > 1 Then
  directory=depth.Get(depth.Size - 2)
  depth.RemoveAt(depth.Size-1)
  buildFileList(directory)
  Return
 End If
 If File.IsDirectory(directory,asLabel(asPane(lv.Items.Get(Index)).GetNode(1)).Text) Then
  directory = directory & asLabel(asPane(lv.Items.Get(Index)).GetNode(1)).Text&"/"
  depth.Add(directory)
  buildFileList(directory)
  Return
 End If
 
 Log("You chose : " & asLabel(asPane(lv.Items.Get(Index)).GetNode(1)).Text)
End Sub
Sub getIcon(s As JavaObject) As Image
 Return swingUtils.RunMethod("toFXImage",Array(fsv.RunMethodJO("getFileSystemView",Null).RunMethodjo("getSystemIcon",Array(s)).RunMethod("getImage",Null),Null))
End Sub
Sub buildPane(fi As JavaObject,r As Boolean) As Pane
 Dim p As Pane
 p.Initialize("")
 Dim iv As ImageView
 iv.Initialize("")
 Dim lab,lab1,lab2 As Label
 lab.Initialize("")
 lab1.Initialize("")
 lab2.Initialize("")
 iv.SetImage(getIcon(fi))
 lab.text = fsv.RunMethodJO("getFileSystemView",Null).RunMethod("getSystemDisplayName",Array(fi))
 lab.TooltipText = lab.text & CRLF & _
       fileSize(fi.RunMethod("length",Null))
 lab1.Text ="["& fsv.RunMethodJO("getFileSystemView",Null).RunMethod("getSystemTypeDescription",Array(fi))&"]"
 Dim offset As Int = 20
 If r Then offset = 0
 p.AddNode(iv,offset+0,0,20,20)
 p.AddNode(lab,offset+25,0,200,20)
 p.AddNode(lab1,250,0,200,20)
 Return p
End Sub
Sub new(className As String,args() As Object) As JavaObject
 Dim j As JavaObject
 Return j.InitializeNewInstance(className,args)
End Sub
Sub asLabel(l As Object) As Label
 Return l
End Sub
Sub asPane(p As Object) As Pane
 Return p
End Sub
Sub fileSize(s As Long) As String
 Dim t As Double
 Dim gb As Long = 1024*1024*1024
 Dim mb As Long = 1024*1024
 Dim kb As Long = 1024
 Select True
  Case s>gb
   t = s/gb
   Return NumberFormat(t,1,0)& "GB"
  Case s>mb
   t = s/mb
   Return NumberFormat(t,1,0)& " MB"
  Case s>kb
   t = s/kb
   Return NumberFormat(t,1,0)& " KB"
  Case Else
   Return s&" Bytes" 
 End Select
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
How to Force FileChooser to one directory and it's sub-folders
Write your own FileChooser (many examples here) and control it as u wish...

Standard helpers lack in so many ways yet they inspire all of us to expand on the basic concept.
When u get to your perfect solution , share your code. We can all benefit.

Thanks so much.
 
Upvote 0
Top