B4J Code Snippet JavaObjects (the easy way)

This is probably the shortest snippet ever, but I find it quite useful

B4X:
Sub asJavaObject(j As JavaObject) As JavaObject
    Return j
End Sub

With it you can turn an object into a javaobject and then run a method on it

B4X:
'canvas1 defined in designer
asJavaObject(canvas1).RunMethod("setWidth",array(100.00))

EventHandlers become easier too
B4X:
    Dim myEventHandler As Object = asJavaObject(ob).CreateEventFromUI("javafx.event.EventHandler", _
                                                        handlerName,False)
    asJavaObject(ob).RunMethod(eventName, Array(myEventHandler))
 

Daestrum

Expert
Licensed User
Longtime User
The code, as written, is how I have used it for ages.
Whatever you give it, it returns a javaobject representation of it, thus allowing use of runmethod on it.
It's the same as
B4X:
Dim jo as javaobject = anobject
jo.runmethod(......)
But in one line.
 

warwound

Expert
Licensed User
Longtime User
The JavaObject is anonymous too, no references are ever made to it.
Nice n clean :).

Martin.
 

Daestrum

Expert
Licensed User
Longtime User
Eventhandler using the method, taken to extreme becomes
B4X:
Sub setHandler(ob As Object,eventName As String,handlerName As String)
    asJavaObject(ob).RunMethod(eventName, Array(asJavaObject(ob).CreateEventFromUI("javafx.event.EventHandler",handlerName,False)))
End Sub
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
The code, as written, is how I have used it for ages.
Whatever you give it, it returns a javaobject representation of it, thus allowing use of runmethod on it.
It's the same as
B4X:
Dim jo as javaobject = anobject
jo.runmethod(......)
But in one line.


Sorry, but it surely returns a javaobject: the javaobject you have passed to the function, without changes!
Sub asJavaObject(j As JavaObject) As JavaObject
Return j
End Sub
 

Daestrum

Expert
Licensed User
Longtime User
Exactly, but you don't pass a javaobject to it, you pass any object to it and it will return a javaobject reference to that object.

if you pass a 'button' to it, it will return a javaobject the same as
B4X:
Dim b As Button
Dim jo As JavaObject = b
' jo is now effectively b
jo.RunMethod(....)

Whereas you can using this method just have

B4X:
Dim b As Button
asJavaObject(b).RunMethod(......)
 

Daestrum

Expert
Licensed User
Longtime User
As we can now use Java code in-line a similar routine makes it easier to use instead of having to declare a JavaObject

The short routine

B4X:
Sub inline as JavaObject
     Return Me
End Sub

Then all the calls to your in-line code become similar to
B4X:
#If JAVA
   public static String myMethod() {
        return "Hello B4J";
   }
#End If

   ....
   Dim s As String = inline.RunMethod("myMethod",Null)
   ....
 

Daestrum

Expert
Licensed User
Longtime User
Been rewriting some code and noticed the code I had in post #6 can be rewritten far clearer
B4X:
Sub setHandler(ob As Object,eventName AsString,handlerName AsString)
asJavaObject(ob).RunMethod(eventName, Array(asJavaObject(ob).CreateEventFromUI("javafx.event.EventHandler",handlerName,False)))
End Sub
can become
B4X:
Sub setHandler(ob As JavaObject,eventName AsString,handlerName AsString)
ob.RunMethod(eventName, Array(ob.CreateEventFromUI("javafx.event.EventHandler",handlerName,False)))
End Sub
thus negating the need for the asJavaObject sub to be called
 

Daestrum

Expert
Licensed User
Longtime User
Found another use for the routine.

If you need a static class to get a field, for example javafx.scene.paint.Color to get the value of "AZURE"

normally you would do something similar to this
B4X:
Dim jo as JavaObject
jo.InitializeStatic("javafx.scene.paint.Color")
Dim myColor As Int = jo.GetField("AZURE")

What you can do is

B4X:
Dim myColor As Int = asJavaObject(Null).InitializeStatic("java.scene.color.Paint").GetField("AZURE")

But, this is a one off code line, you cannot refer to it later so if you need lots of calls, use the first method.
 
Top