B4J Question Custom views cannot be traversed

yshzsl

Member
I created a custom view ImageButton and added several ImageButton in Pane that will work properly.
In the class module of the custom view, mBase is defined as B4xView.
But cannot be traversed in auto scaling code. runtime throws error.
The traversal code is as follows:
B4X:
Private Sub B4XPage_Appear
    For Each n As B4XView In Root.As(Pane)
        n.Height=n.Height*Scale
        n.Top=n.Top*Scale
        n.Left=n.Left*Scale
        If n Is MenuBar Then
            n.TextSize=n.TextSize*Scale
        End If
    Next
    For Each n1 As ImageButton In Pane1
        n1.TextSize=n.TextSize*Scale
        n1.Width=n.Width*Scale
        n1.Height=n.Height*Scale       
    Next
End Sub

The error thrown is as follows:
B4X:
Error occurred on line: 53 (B4XMainPage)
java.lang.ClassCastException: class anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane cannot be cast to class b4j.example.imagebutton (anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane and b4j.example.imagebutton are in unnamed module of loader 'app')
    at b4j.example.b4xmainpage._b4xpage_appear(b4xmainpage.java:96)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:513)
    at anywheresoftware.b4a.keywords.Common.access$0(Common.java:493)
    at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:567)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:832)

It seems that B4xView and ImageButton cannot convert to each other.
 

yshzsl

Member
thanks for your help,walt61.
I tried it with your code and really stopped throwing errors .But the code does not work on the custom ImageButton, and the ImageButton does not change.
Since there are not many ImageButton in the program, I tried to implement automatic scaling directly with the object method, which is a roundabout way to solve this problem.
Code is as follows:
B4X:
Dim row(8) As Object=Array As ImageButton(SetTime,Srjz,Zcjz,Lszcx,Szflcx,Ndhzcx,Jsq,Rjb)
    For i=0 To 7
        Dim a As ImageButton=row(i)
        a.TextSize=a.TextSize*Scale
        a.Width=a.Width*Scale
        a.Height=a.Height*Scale
        a.Top=a.Top*Scale
        a.Left=a.Left*Scale
    Next

Your train of thought has inspired me,
I autoscaled in your way on another page without custom views, The code is a lot more concise.
Thank you again.
 
Upvote 0

walt61

Active Member
Licensed User
Longtime User
You're welcome, @yshzsl ; you can make the code a bit more concise like this (and it's easier then to add views to the list too as you don't have to deal with the dimension of the array):
B4X:
    For Each a As ImageButton In Array As Object(SetTime,Srjz,Zcjz,Lszcx,Szflcx,Ndhzcx,Jsq,Rjb)
        a.TextSize=a.TextSize*Scale
        a.Width=a.Width*Scale
        a.Height=a.Height*Scale
        a.Top=a.Top*Scale
        a.Left=a.Left*Scale
    Next
 
Upvote 0

yshzsl

Member
Thanks for the guidance, walt61.
This makes the code simpler and the thinking clearer. I will do this idea when I write the code later.

The window adaptive code I created with your ideas is really well,But I accidentally found a problem, I do not know how to solve, please help guide.
The problem is something like this:
If I put the code into the B4XPage_Appear module, it appears normal when opening the page,But after minimizing the page once, both the Pane and MenuBar will automatically revert to their original location and size.
If the code is put into the B4XPage_Created module, the Pane and MenuBar of the page are the original location and size of the page, without change.
If the code is put into the B4XPage_Appear module, then add the following code to the B4XPage_IconifiedChanged module:
B4X:
Private Sub B4XPage_IconifiedChanged (Iconified As Boolean)
    If Iconified=False Then
        If State=0 Then
            Resize_Scale(Scale) 'Resize_Scale Is the zoom code module
        End If           
    Else   
        Resize_Scale(1/Scale)
        State=0
    End If
End Sub
After the page is minimized several times, the size and position of the page elements deviate too much from the expected distance, and the page is too messy.
Please help me find out the solution to the problem. Thank you very much.
 
Upvote 0

yshzsl

Member
Thanks so much for your concern and attention, walt61.
Now the B4xMainPage zoom function has been basically implemented,But not sure what to do with the zoom of the shz page,No matter what method, the position and size of the page elements do not change, always the original position and size.
Now I upload the code made so far,Expect you to take time out of your busy schedule to check it,See where the problem is.
I am a novice, do not understand too much, please give more guidance.
 

Attachments

  • jtjzTest.zip
    474 KB · Views: 14
Upvote 0

walt61

Active Member
Licensed User
Longtime User
First thing I noticed was that you missed something from post #2, i.e. its first line:
B4X:
For Each n As B4XView In root.GetAllViewsRecursive ' NOT: '... In root.As(Pane)' or '... In Pane2'

But as you don't allow the form to be resized by the user, only maximised or iconified, I'm not sure what you're trying to accomplish by using the Scale variable; can you elaborate a bit on that?
 
Upvote 0

yshzsl

Member
Hello, walt61. Thank you for your help.
I like to write code on the big TV screen. I look at the font size being just right when the large TV screen zoom rate is 150%.
Families like to operate on their computers,This code on the computer will be small and uncomfortable to watch.
I just want to add some window adaptive code,The font and controls are the same size when you open the software at any resolution.

Also, I forgot to explain it.After I added Textarea and table in the shz page,Using root.GetAllViewsRecursive always throws the index 12 out of range,Have not found the error yet,I change back to the original code temporarily.

Is there a better window adaptive code ideas?
 
Upvote 0

walt61

Active Member
Licensed User
Longtime User
I see. Regarding the index out of range exception: it doesn't do that here. Are you sure you changed all the 'For Each' lines in methods 'getsizes' and 'Resize_Scale' in both pages?

Regarding the scaling: that's not my forte I'm afraid. Judging from your code I think you already had a look at @klaus 's booklets and have experimented with the Designer Scripts' Autoscale... commands; I can't test that here with just the one monitor I use. I suggest you open a new thread for that question (e.g. 'How to automatically scale views on different monitor sizes') so that someone who knows what they're doing in circumstances like yours can pick up on it :)
 
Upvote 0

yshzsl

Member
Thank you, walt61.Thank you for your tireless guidance and help.
I will check if the number of rows read by getsizes corresponds to the number of rows set by Resize_Scale.
Your suggestions for autoscaling reminds me,The designer's AutoScale script may fit the view,I will try to see if it is any easier to solve this problem.

Thank you once again, walt61.
 
Upvote 0
Top