B4J Question Toolbar in B4J

Discussion in 'B4J Questions' started by melliott, Apr 14, 2015.

  1. melliott

    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
     
  2. rwblinn

    rwblinn Well-Known Member Licensed User

    Hi,

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

    Code:
    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, 
    101030050)
    End Sub

    Sub B1_Action
        
    Log("Button1 clicked"
    End Sub

    Sub B2_Action
        
    Log("Button2 clicked"
    End Sub
     
    Last edited: Apr 14, 2015
  3. melliott

    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!
     
  4. rwblinn

    rwblinn Well-Known Member Licensed User

    Hi,

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

    melliott Member Licensed User

    rwblin that worked perfect! Thanks.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice