### Number TextField

Example of a Custom View class - NumberTextField
B4J Custom View Class
' Set the Designer Properties
#DesignerProperty: Key:FieldValue, Displayname: Field Value, Fieldtype: String, Defaultvalue: , Defaultvalue: 0, Description: The NumberTextField Field
#DesignerProperty: Key:FieldPrompt, DisplayName: Field Prompt, FieldType: String, DefaultValue: , Description: The NumberTextField Prompt
`Private Sub Class_Globals
Private Version As String = "v20161010"
Private fx As JFX
Private mEventName As String 'ignore
Private mCallBack As Object 'ignore
Private mBase As Pane
Private mProps As Map 'ignore
Private tfNumeric As TextField
Private btnClear As Button
End Sub`
`Public Sub Initialize (vCallback As Object, vEventName As String)
mEventName = vEventName
mCallBack = vCallback
End Sub`
`Public Sub DesignerCreateView (Base As Pane, Lbl As Label, Props As Map)
mBase = Base
mProps = Props
CSSUtils.SetBorder(mBase, 0, fx.Colors.Transparent, 0)
tfNumeric.Initialize("tfNumeric")
tfNumeric.Style = Lbl.Style
mBase.AddNode(tfNumeric, 0, 0, 0, 0)
btnClear.Initialize("btnClear")
CSSUtils.SetBackgroundImage(btnClear, File.DirAssets, "clear.png")
CSSUtils.SetStyleProperty(btnClear, "-fx-background-position", "center")
mBase.AddNode(btnClear, 0, 0, 0, 0)
End Sub`
`Public Sub AddToParent(Parent As Pane, Left As Int, Top As Int, Width As Int, Height As Int)
mBase.Initialize("mBase")
tfNumeric.Initialize("tfNumeric")
mBase.AddNode(tfNumeric, 0, 0, 0, 0)
btnClear.Initialize("btnClear")
mBase.AddNode(btnClear, 0, 0, 0, 0)
CSSUtils.SetBackgroundImage(btnClear, File.DirAssets, "clear.png")
Parent.AddNode(mBase, Left, Top, Width, Height)
Base_Resize (Width, Height)
End Sub`
`Private Sub Base_Resize (Width As Double, Height As Double)
tfNumeric.SetLayoutAnimated(0, 4, 0, Width - 30, Height)
btnClear.SetLayoutAnimated(0, Width - 30, 0, 30, Height)
End Sub`
' Subroutine: Get or set the width of the base
`Public Sub SetWidth(Width As Double)
If Width < 100 Then Width = 100
mBase.PrefWidth = Width
End Sub`
`Public Sub GetWidth As Double
Return mBase.PrefWidth
End Sub`
' Subroutine: Get or set the height of the base
`Public Sub SetHeigth(Height As Double)
If Height < 10 Then Height = 10
mBase.PrefHeight=Height
End Sub`
`Public Sub GetHeigth As Double
Return mBase.PrefHeight
End Sub`
' Property: Get or set the top position of the base
`Public Sub setTop(Top As Double)
If Top < 0 Then Top = 0
mBase.Top = Top
End Sub`
`Public Sub getTop As Double
Return mBase.Top
End Sub`
' Property: Get or set the left position of the base
`Public Sub setLeft(Left As Double)
If Left < 0 Then Left = 0
mBase.Left = Left
End Sub`
`Public Sub getLeft As Double
Return mBase.Left
End Sub`
' Property: Get or set the tag of the base
`Public Sub setTag(Tag As String)
mBase.Tag = Tag
End Sub`
`Public Sub getTag As String
Return mBase.Tag
End Sub`
' Property: Get or set the visibility of the base
`Public Sub setVisible(Visible As Boolean)
mBase.Visible = Visible
End Sub`
`Public Sub getVisible As Boolean
Return mBase.Visible
End Sub`
' Property: Get or set value of the textfield
`Public Sub getValue As String
Return tfNumeric.Text
End Sub`
`Public Sub setValue(Value As String)
tfNumeric.Text = Value
End Sub`
' Property: Get or set value of the textfield tooltip
`Public Sub setTooltipText(Value As String)
tfNumeric.TooltipText = Value
End Sub`
`Public Sub getTooltipText As String
Return tfNumeric.TooltipText
End Sub`
`Private Sub tfNumeric_TextChanged (Old As String, New As String)
If Not(IsNumber(New)) And (New.Length > 0) Then tfNumeric.Text = Old
End Sub`
`Private Sub btnClear_Action
tfNumeric.Text = ""
End Sub`
`Public Sub Clear
tfNumeric.Text = ""
End Sub`
' Property: Get or set value of the textfield tooltip
`Public Sub setButtonTooltipText(Value As String)
btnClear.TooltipText = Value
End Sub`
`Public Sub getButtonTooltipText As String
Return btnClear.TooltipText
End Sub`
`Public Sub GetVersion As String
Return Version
End Sub`
B4J Example using the Custom View Class
`Sub Process_Globals
Private fx As JFX
Public MainForm As Form
Private btnShowFieldContent As Button
Private NumberTextField1 As NumberTextField
Private NumberTextField2 As NumberTextField
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UTILITY")
MainForm.RootPane.LoadLayout("Main")
MainForm.Title = "B4J HowTo - Custom View NumberTextField"
MainForm.Show
CallSubDelayed(Me, "Resize")
NumberTextField2.Initialize(Me, "LabeledTextField2")
NumberTextField2.AddToParent(MainForm.RootPane, 20, 200, 100,30)
NumberTextField2.Value = "99"
End Sub`
`Sub Resize
CallSubDelayed2(NumberTextField1, "SetWidth", 100)
NumberTextField1.Top = 20
NumberTextField1.Left = 20
NumberTextField1.Heigth = 30
NumberTextField1.Value = 9
NumberTextField1.TooltipText = "Enter a numeric value"
NumberTextField1.ButtonTooltipText = "Clear the text"
End Sub`
`Sub btnShowFieldContent_Action
fx.Msgbox(Null, $"The content of the NumberTextField:${CRLF}${NumberTextField1.GetValue}${CRLF}${NumberTextField2.GetValue}"$, "Info")
End Sub`

Changed: , Category: Custom Views, File: T6C179M1300.htm, ID: 1300