B4J Question B4J Measure Text Width problem

demasi

Active Member
Licensed User
Longtime User
Hello,
I´m having a problem with this solution to measure text width in B4J.

original post link: https://www.b4x.com/android/forum/threads/measure-text.45750/#content

B4X:
Private Sub MeasureTextWidth (Text As String, Font As B4XFont) As Float
   Dim lbl As Label
   lbl.Initialize("")
   lbl.Text = Text
   lbl.Font = Font
   mBase.AddView(lbl, 0, 0, -1, -1) 'mBase is a B4XView panel.
   lbl.Snapshot
   lbl.RemoveNodeFromParent
   Return lbl.Width
End Sub

When I use a very large text with a big text size, it gives an error message at the "lbl.snapshot" line.
With small texts or small fonts it works ok.
I need to use large texts (about 200 chars each message), with fontsize at least 60 and label that have the screen width (1920 pixels).
I modified the class ScrollingLabel to scroll the messages as a news ticker. But with font size less than 60 the text is only visible near the Tv.

these are the messages at the log:

Waiting for debugger to connect...
Program started.
Error occurred on line: 112 (ScrollingLabel)
java.lang.IllegalArgumentException: Unrecognized image loader: null
at javafx.scene.image.WritableImage.loadTkImage(WritableImage.java:240)
at javafx.scene.image.WritableImage.access$000(WritableImage.java:46)
at javafx.scene.image.WritableImage$1.loadTkImage(WritableImage.java:51)
at javafx.scene.Scene.doSnapshot(Scene.java:1236)
at javafx.scene.Node.doSnapshot(Node.java:1862)
at javafx.scene.Node.snapshot(Node.java:1940)
at anywheresoftware.b4j.objects.NodeWrapper.Snapshot2(NodeWrapper.java:318)
at anywheresoftware.b4j.objects.NodeWrapper.Snapshot(NodeWrapper.java:309)
at b4j.example.scrollinglabel._measuretextwidth(scrollinglabel.java:291)
at b4j.example.scrollinglabel._settext(scrollinglabel.java:125)
at b4j.example.main._scrollinglabel1_settext(main.java:739)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:482)
at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:429)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:19)
at anywheresoftware.b4a.keywords.Common$2$1.run(Common.java:1013)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)


Do someone know a solution for this?
Thank you
 

demasi

Active Member
Licensed User
Longtime User
Thank you Erel.
Just to help others that possibly get this error in a project, I solved this in a not so beautiful way, but works anyway, I separated the message in words and sum each word's measure.
I'll try java 9 too.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Hi demasi, I've added another option to the MeasureText thread which appears to do what you want.
 
Upvote 0
Top