B4J Question Toolbar in B4J

melliott

Member
Licensed User
Longtime User
Hello,

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


Thanks,

Michael
 
Hi,

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

Code: Copy
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:
Upvote 0
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!
 
Upvote 0
Hi,

can be done, by making joToolBar global
Code: Copy
Sub Process_Globals
...
    Dim joToolBar As JavaObject
...
End Sub

then
Code: Copy
Sub MainForm_Resize (Width As Double, Height As Double)
    joToolBar.RunMethod("setPrefWidth", Array(Width))
End Sub
 
Upvote 0
Back
Top