### Sample 3

Another sample application showing the use of a toolbar with buttons (requires the JavaObject library).
#Region Project Attributes
#MainFormWidth: 400
#MainFormHeight: 300
#End Region
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
'Define the toolbar JavaObject
Private joToolbar As JavaObject
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UTILITY")
MainForm.RootPane.LoadLayout("Main")
MainForm.Title = "B4J HowTo Toolbar v20160325"
MainForm.Show
'
AppInit
End Sub`
`Sub AppInit
ToolBar_Create
End Sub`
'Close the app - add any specifics here
`Sub AppClose
MainForm.Close
ExitApplication
End Sub`
'Handle form closing via system close (top right X button)
`Sub MainForm_CloseRequest (EventData As Event)
AppClose
End Sub`
'Handle resizing of the mainform which adjusts the width of the toolbar object
`Sub MainForm_Resize (Width As Double, Height As Double)
joToolbar.RunMethod("setPrefWidth", Array(Width))
End Sub`
Region TOOLBAR
`Sub ToolBar_Create
'Init the toolbar object
joToolbar.InitializeNewInstance("javafx.scene.control.ToolBar", Null)
'Create list of buttons to add to the toolbar
Dim l As List
l.Initialize
'Add the buttons using sub Toolbar_Button.
'The tag is used to determine which action to execute in sub ToolbarButton_Action. The tag must be unique per button.
l.Add(ToolBar_Button("Button 1", 1, fx.Colors.Blue, "white"))
l.Add(ToolBar_Button("Button 2", 2, fx.Colors.White, "red"))
l.Add(ToolBar_Button("About", 3, Null, ""))
'Add the list of toolbar buttons to the JavaObject
joToolbar.RunMethodJO("getItems", Null).RunMethod("addAll", Array(l))
'Add the toolbar JavaObject to the mainform with a height of 30.0
MainForm.RootPane.AddNode(joToolbar, 0, 0, MainForm.Width, 30.0)
End Sub`
'Add a button to the toolbar
`Sub ToolBar_Button(BtnText As String, BtnTag As Object, BtnColor As Paint, BtnTextColor As String) As Button
Dim b As Button
b.Initialize("ToolBarButton")
b.Tag = BtnTag
b.Text = BtnText
If BtnColor.IsInitialized Then CSSUtils.SetBackgroundColor(b, BtnColor)
If BtnTextColor.Length > 0 Then CSSUtils.SetStyleProperty(b, "-fx-text-fill", BtnTextColor)
b.PrefWidth = 100
Return b
End Sub`
'Handle ToolbarButton Actions by selecting the unique button tag
`Sub ToolBarButton_Action
Dim b As Button = Sender
Select b.Tag
Case 1
Button1_Action
Case 2
Button2_Action
Case 3
About_Action
End Select
End Sub`
End Region
Region ACTIONS
`Sub Button1_Action
fx.Msgbox2(MainForm,"Toolbar Button 1 clicked", "Information", "OK", "", "", fx.MSGBOX_INFORMATION)
End Sub`
`Sub Button2_Action
fx.Msgbox2(MainForm,"Toolbar Button 2 clicked", "Warning", "OK", "", "", fx.MSGBOX_WARNING)
End Sub`
`Sub About_Action
fx.Msgbox2(MainForm,MainForm.title, "About", "OK", "", "", fx.MSGBOX_INFORMATION)
End Sub`
End Region

Changed: , Category: ToolBar, File: T6C270M2007.htm, ID: 2007