B4J Question Access Object from string

rossati

Active Member
Licensed User
Longtime User
Hello

I have a string where a fragment is:
B4X:
Window,${Main.MainForm},ff008000 ff0000ff,shadow;
MainForm is a form of Main module, in the string it appears as anywheresoftware.b4j.objects.Form@123bfb94
How can I access the above form from the string?
Thanks
 

Daestrum

Expert
Licensed User
Longtime User
(Sorry to disagree Erel)
Java reflection enables you to get the object from it's address (anywheresoftware.b4j.objects.Form@123bfb94)

I can post an example if you want.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I used the wrong term.


B4X:
...
    Dim m As Map
    Dim s As String
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    ' make some other screens
    anotherForm.Initialize("",100,100)
    yetAnother.Initialize("",100,100)
    ' init the map
    m.Initialize
    'get object name of mainform
    Log("The one I want <" & MainForm & ">")
    s = MainForm ' naughty but nice
End Sub

Sub Button1_Click
    (Me).As(JavaObject).RunMethod("doIt",Array(Me,m))
    ' read map for the form object and then change title.
    m.Get(s).As(Form).Title = "Hello"
End Sub
#if java
import java.util.*;
import java.lang.reflect.*;

public static Map<String,Object> doIt(Class c,Map m) throws Exception{
    var f = c.getDeclaredFields();
    for (var fld : f){
        var wanted = fld.toString();
        if (wanted.contains(".Form")){
            m.put(fld.get(c).toString(),fld.get(c));
        }
    }
    return m;
}
#End If

The java without using var
B4X:
public static Map<String,Object> doIt(Class c,Map m) throws Exception{
    Field[] f = c.getDeclaredFields();
    for (Field fld : f){
        String wanted = fld.toString();
        if (wanted.contains(".Form")){
            m.put(fld.get(c).toString(),fld.get(c));
        }
    }
    return m;
}
 
Last edited:
Upvote 0

rossati

Active Member
Licensed User
Longtime User
Thanks to all, in any case this increases my knowledge of the Java and B4x environment.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…