I want to record this before I forget it, so:
A context menu created in code seems to live outside of the MainForm.Rootpane's path when it comes to the CSS. So to add it we need to add it to the MainForm.RootPanes scene.
A working example:
And some CSS to access the cmenu class we created, in a file (cmenu.css) in the assets directory:
Now I know where to find it next time.
A context menu created in code seems to live outside of the MainForm.Rootpane's path when it comes to the CSS. So to add it we need to add it to the MainForm.RootPanes scene.
B4X:
Dim JO As JavaObject = MainForm.RootPane
JO.RunMethodJO("getScene",Null).RunMethodJO("getStylesheets",Null).RunMethod("add",Array(File.GetUri(File.DirAssets,"cmenu.css")))
A working example:
B4X:
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private CMenu As ContextMenu
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
CMenu.Initialize("CM")
'Add the CSS file to the RootPane's Scene
Dim JO As JavaObject = MainForm.RootPane
JO.RunMethodJO("getScene",Null).RunMethodJO("getStylesheets",Null).RunMethod("add",Array(File.GetUri(File.DirAssets,"cmenu.css")))
'Add a style class to the context menu
Dim CMJO As JavaObject = CMenu
CMJO.RunMethodJO("getStyleClass",Null).RunMethod("add",Array("cmenu"))
'Add some items to the menu
Dim Text() As String = Array As String("A","B","C")
For i = 0 To Text.Length - 1
Dim MItem As MenuItem
MItem.Initialize(Text(i),"MI")
CMenu.MenuItems.Add(MItem)
Next
MainForm.Show
End Sub
'Trigger the context menu from a click on the form
Sub MainForm_MouseClicked (EventData As MouseEvent)
Dim CMJO As JavaObject = CMenu
Dim XOffset As Double = -MainForm.RootPane.Width + EventData.X
Dim YOffset As Double = EventData.Y
CMJO.RunMethod("show",Array(MainForm.RootPane,"RIGHT",XOffset,YOffset))
End Sub
'Respond to the click
Sub MI_Action
Dim MItem As MenuItem = Sender
Log(MItem.Text)
End Sub
And some CSS to access the cmenu class we created, in a file (cmenu.css) in the assets directory:
B4X:
.cmenu {
-fx-background-color: blue;
}
.cmenu .label {
-fx-font-size: 20px;
-fx-text-fill: yellow;
}
Now I know where to find it next time.
Last edited: