B4J Question Toolbar in B4J

melliott

Member
Licensed User
Hello,

How do you create a ToolBar (AKA ButtonBar) in B4J using all code (not using Screen Builder that is)?


Thanks,

Michael
 

rwblinn

Well-Known Member
Licensed User
Hi,

one possible solution using JavaObject Library.
Call CreateToolBar in f.e. AppStart after MainForm.Show.

B4X:
Sub CreateToolBar
    Dim joToolBar As JavaObject
    Dim B1 As Button:B1.Initialize("B1"):B1.Text="Button 1":B1.TextColor=fx.Colors.Red
    Dim B2 As Button:B2.Initialize("B2"):B2.Text="Button 2":B2.TextColor=fx.Colors.Blue
    joToolBar.InitializeNewInstance("javafx.scene.control.ToolBar", Null)
    joToolBar.RunMethodJO("getItems", Null).runMethod("add", Array(B1))
    joToolBar.RunMethodJO("getItems", Null).runMethod("add", Array(B2))
    joToolBar.RunMethod("setStyle", Array("-fx-background-color: yellow;"))
    MainForm.RootPane.AddNode(joToolBar, 10, 10, 300, 50)
End Sub

Sub B1_Action
    Log("Button1 clicked") 
End Sub

Sub B2_Action
    Log("Button2 clicked") 
End Sub
 
Last edited:

melliott

Member
Licensed User
Rob thanks that is great!!!

One follow on question please. Upon a MainForm_Resize event what code would I use to resize the Toolbar width to the new MainForm.Width?

Thanks so much!
 

rwblinn

Well-Known Member
Licensed User
Hi,

can be done, by making joToolBar global
B4X:
Sub Process_Globals
...
    Dim joToolBar As JavaObject
...
End Sub
then
B4X:
Sub MainForm_Resize (Width As Double, Height As Double)
    joToolBar.RunMethod("setPrefWidth", Array(Width))
End Sub
 
Top