B4J Question Filter data entry into a Text Field

desof

Well-Known Member
Licensed User
Longtime User
As it is possible to filter keystrokes on a TextField just numbers ?
 

Daestrum

Expert
Licensed User
Longtime User
yes - using robots (not joking - glass.ui.application robots) see code below.
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private tf1 As TextField
    Private robot,temp As JavaObject
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    robot = temp.InitializeStatic("com.sun.glass.ui.Application").RunMethodJO("GetApplication",Null).RunMethodJO("createRobot",Null)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("tf1") 'Load the layout file. just a textfield
    MainForm.Show
End Sub
Sub tf1_TextChanged (Old As String, New As String)
     If New.Length > 0 Then
        If "0123456789".Contains(New.SubString2(New.Length-1,New.Length)) = False Then
            robot.RunMethod("keyPress",Array As Object(8)) '  send backspace to field if alpha character
        End If
     End If
End Sub
 
Last edited:
Upvote 0

desof

Well-Known Member
Licensed User
Longtime User
Por que?

B4X:
Program started.
Error occurred on line: 62 (main).
java.lang.IllegalAccessException: Class anywheresoftware.b4j.object.JavaObject can not access a member of class com.sun.glass.ui.win.WinApplication with modifiers "public"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:109)
    at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
    at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
    at java.lang.reflect.Method.invoke(Method.java:599)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:80)
    at anywheresoftware.b4j.object.JavaObject.RunMethodJO(JavaObject.java:87)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:468)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:209)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:153)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:92)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:69)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:83)
    at b4j.example.main.start(main.java:33)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
    at java.lang.Thread.run(Thread.java:744)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
That is odd, I have never had that exception.
If I log(robot) after initialize I get
(WinRobot) com.sun.glass.ui.win.WinRobot@1ee1619
Is the execption on the initialize or the keyPress line ?
 
Upvote 0

desof

Well-Known Member
Licensed User
Longtime User
That is odd, I have never had that exception.
If I log(robot) after initialize I get
(WinRobot) com.sun.glass.ui.win.WinRobot@1ee1619
Is the execption on the initialize or the keyPress line ?


B4X:
Sub AppStart (Form1 As Form, Args() As String)
    robot = temp.InitializeStatic("com.sun.glass.ui.Application").RunMethodJO("GetApplication",Null).RunMethodJO("createRobot",Null)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main")
    MainForm.SetFormStyle("UTILITY")
    MainForm.Show

La linea 64 es el error
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I take it you are using JavaObject Library v1.00
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I will see if I can recreate the exeception

the error seems somehow related to security policy files. Out of my knowledge , sorry.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code:
B4X:
Sub txtField1_TextChanged (Old As String, New As String)
   Dim sb As StringBuilder
   sb.Initialize
   For i = 0 To New.Length - 1
     If "0123456789".IndexOf(New.CharAt(i)) > -1 Then
       sb.Append(New.CharAt(i))
     End If
   Next
   txtField1.Text = sb.ToString
   txtField1.SetSelection(txtField1.Text.Length, txtField1.Text.Length)
End Sub
 
Upvote 0

desof

Well-Known Member
Licensed User
Longtime User
You can use this code:
B4X:
Sub txtField1_TextChanged (Old As String, New As String)
   Dim sb As StringBuilder
   sb.Initialize
   For i = 0 To New.Length - 1
     If "0123456789".IndexOf(New.CharAt(i)) > -1 Then
       sb.Append(New.CharAt(i))
     End If
   Next
   txtField1.Text = sb.ToString
   txtField1.SetSelection(txtField1.Text.Length, txtField1.Text.Length)
End Sub

GENIAL !!!!!!
 
Upvote 0
Top