Here is a complete system with hard drive and USB key and ContextMenu
Adapt it to your code. If you still have problems please ask again
[CODE lang="b4x" title="B4J"] 'TreeView variable
Private TreeView1 As TreeView
Private rootdir As String
Private CurrentDrive As String
Private CurrentPath As String
Private CurrentFile As String
Private lblTreeviewPath As Label
Private lineTRV As Pane
Private CtMenu As ContextMenu[/CODE]
[CODE lang="b4x" title="B4J"]#Region TreeView
Private Sub DriveList
'List all PC drives and add them to the CLV
Dim mDrive As String
Dim fsv As JavaObject
fsv.InitializeStatic("javax.swing.filechooser.FileSystemView")
For Each drive As Object In ListRoots
mDrive = fsv.RunMethodJO("getFileSystemView",Null).RunMethod("getSystemDisplayName",Array(drive)) 'Get Drive Name
clvDrive.Add(ItemDrive(clvDrive.AsView.Width,mDrive),drive) 'add Item
Next
End Sub
Sub ListRoots As List
Dim jo As JavaObject
Dim o() As Object = jo.InitializeStatic("java.io.File").RunMethod("listRoots", Null)
Return o
End Sub
Private Sub ItemDrive(Width As Int, s As String) As Pane
Dim p As B4XView = xui.CreatePanel("")
Dim height As Int = 20dip
p.SetLayoutAnimated(0, 0, 0, Width, height)
p.Color = xui.Color_Transparent
p.LoadLayout("itemDrive")
lblDrive.Text = s
Return p
End Sub
Private Sub clvDrive_ItemClick (Index As Int, Value As Object)
pn_5.As(B4XView).BringToFront
CurrentDrive = Value
LoadTreeview(Value)'drive selected
End Sub
Private Sub LoadTreeview(mDrive As String)
TreeView1.Root.Children.Clear
rootdir = mDrive
lblTreeviewPath.text = rootdir
lblTreeviewPath.TooltipText = rootdir
Private FolderImage As Image
FolderImage = fx.LoadImage(File.DirAssets, "folderico.png")
AddFolder(TreeView1.Root, mDrive, FolderImage)
End Sub
Sub AddFolder(Parent As TreeTableItem, Folder As String, img As Image)
TreeView1.Root.Children.Clear
CurrentPath = Folder
'first item to go back
Dim ti As TreeItem
ti.Initialize(Folder,"...")
ti.Image = xui.LoadBitmap(File.DirAssets,"folderback.png")
Parent.Children.Add(ti)
'All Item
For Each f As String In File.ListFiles(Folder)
Dim ti As TreeItem
Dim Name As String = f
ti.Initialize("ti", Name)
If File.IsDirectory(Folder, f) Then
'Folder
Parent.Children.Add(ti)
ti.Expanded = False
ti.Image = img
Else
'File - Only shows files with the extension
Dim extension As String = ti.text
extension = extension.SubString(extension.LastIndexOf(".") )
If extension = ".wav" Or extension = ".mp3" Or extension = ".ogg" Or extension = ".flac" Then
Parent.Children.Add(ti)
ti.Image = xui.LoadBitmapResize(File.DirAssets,"soundICO.png",16,16,True)
End If
End If
Next
End Sub
Private Sub TreeView1_SelectedItemChanged (SelectedItem As TreeItem)
If SelectedItem.IsInitialized = False Then Return
Dim ti As TreeItem = SelectedItem
If SelectedItem.Text = "..." Then 'First item clicked - go back
' Log("here"&CurrentPath)
If CurrentPath = CurrentDrive Then Return
CurrentPath = CurrentPath.SubString2(0, CurrentPath.LastIndexOf("\"))
If CurrentPath.Contains("\") = False Then CurrentPath = CurrentPath & "\"
LoadTreeview(CurrentPath)
Else If File.IsDirectory(rootdir, ti.Text) = True Then 'If it is a folder on list of subfolders
CurrentPath = CurrentPath&"\"&SelectedItem.text
CurrentPath = CurrentPath.Replace("\\","\")
' Log(CurrentPath)
LoadTreeview(CurrentPath)'list of subfolders
Else
'Let's test the file extension to figure out if its an image
Dim extension As String = ti.text
extension = extension.SubString(extension.LastIndexOf(".") )
Select extension
Case ".wav", ".mp3",".ogg",".flac"
CurrentFile = ti.text 'FileName
End Select
End If
End Sub
Private Sub TreeView1_MouseClicked (EventData As MouseEvent)
pn_5.As(B4XView).BringToFront
If EventData.PrimaryButtonPressed =True Then
'Leftmouse clicked
If File.IsDirectory(CurrentPath, CurrentFile) = False Then' Return
'Save the current Path
File.WriteString(File.DirApp, "path.txt", CurrentPath)
'Convert the file to load it into AudioClip
'Preview selected sound
convert(CurrentPath,CurrentFile,"treeview")
End If
End If
If EventData.SecondaryButtonPressed =True Then
'Rightmouse clicked
If File.IsDirectory(CurrentPath, CurrentFile) Then Return
'Load context menu if it's a sound file only
Dim CMJO As JavaObject = CtMenu
Dim XOffset As Double = -TreeView1.Width + EventData.X
Dim YOffset As Double = EventData.Y
CMJO.RunMethod("show",Array(TreeView1,"RIGHT",XOffset,YOffset))
End If
End Sub
'Right Click mouse - ContextMenu Choice
Sub MI_Action
Dim MItem As MenuItem = Sender
If MItem.Text.Contains("Beatmaker") Then
Log("beatMaker : "&CurrentPath&"---"&CurrentFile)
convert(CurrentPath,CurrentFile,"Beatmaker")
End If
If MItem.Text.Contains("track") Then
Log("pianoroll : "&CurrentPath&"---"&CurrentFile)
convert(CurrentPath,CurrentFile,"Track")
End If
End Sub
Private Sub lblTreeviewPath_MousePressed (EventData As MouseEvent)
pn_5.As(B4XView).BringToFront
End Sub
#End Region[/CODE]