### Use Modal Forms

B4J supports Modal Dialogs or Forms.
If a modal form is called, the calling code (which could be another form) will only continue after the modal form is closed.
To showing a modal form use Form.ShowAndWait.
This example is based upon the tutorial given by B4J.
Principle
MainForm calls the ModalForm to select a Color.
If color selected, then MainForm Background Color is changed and MainForm title shows the selected color name.
'
' B4J MAINFORM
'
#Region Project Attributes
#MainFormWidth: 300
#MainFormHeight: 200
#End Region
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UTILITY")
MainForm.RootPane.LoadLayout("ModalExample")
MainForm.Show
End Sub`
'Button to call the ModalForm SelectColorForm
`Sub btnColor_Action
Dim scf As SelectColorForm
scf.Initialize(MainForm)
' Show the selectcolorform and wait till a color is selected.
' A color must be selected - see SelectColorForm
' Retured are the color, but also the public SelectColorForm variable colorName is set
MainForm.BackColor = scf.Show
' The selected colorName is used to set the MainForm Title
MainForm.Title = scf.colorName
End Sub`
Layout Nodes
Button btnColor
'
' B4J CLASS: SELECTCOLORFORM
'
'Class module for SelectColorForm
`Sub Class_Globals
Private fx As JFX
Private colorsMap As Map ' List of colors as a map (colrname, colorvalue)
Dim lstColors As ListView ' List view holdinf the colors
Private frm As Form
Public colorName As String
End Sub`
' Init the form. The MainForm is passed as parameter
`Public Sub Initialize (Parent As Form)
frm.Initialize("frm", 300dip, 200dip)
frm.Title = "Select Color"
frm.SetFormStyle("UTILITY")
frm.RootPane.LoadLayout("SelectColorForm")
'
FillColorList
End Sub`
' Close request handling - a color must be selected! if not the form consumes
`Sub frm_CloseRequest (EventData As Event)
If lstColors.SelectedIndex = -1 Then EventData.Consume
End Sub`
' Fill the list of colors by populating the coloesMap
`Private Sub FillColorList
colorsMap.Initialize
colorsMap.Put("Red", fx.Colors.Red)
colorsMap.Put("Black", fx.Colors.Black)
colorsMap.Put("Blue", fx.Colors.Blue)
colorsMap.Put("Cyan", fx.Colors.Cyan)
colorsMap.Put("DarkGray", fx.Colors.DarkGray)
colorsMap.Put("Green", fx.Colors.Green)
colorsMap.Put("Magenta", fx.Colors.Magenta)
colorsMap.Put("Yellow", fx.Colors.Yellow)
colorsMap.Put("White", fx.Colors.White)
' Add the Keys = Colorname to the list
For Each color As String In colorsMap.Keys
lstColors.Items.Add(color)
Next
End Sub`
' Show the form and return the selected color (type Paint) taken from the map (key = value)
' Returns: 1) Selected color as Paint; 2) Sets the colorName (Public var)
`Public Sub Show As Paint
frm.ShowAndWait
colorName = lstColors.SelectedItem
Return colorsMap.Get(lstColors.SelectedItem)
End Sub`
' If a color is selected, close the form directly
`Sub lstColors_SelectedIndexChanged(Index As Int)
If Index > -1 Then frm.Close
End Sub`
Layout Node(s)
ListView lstColors

Changed: 2017-10-24 15:14, Category: Forms, File: T6C195M1437.htm, ID: 1437