Sep 27, 2014 Replies: 11 This is probably the shortest snippet ever, but I find it quite useful Code: Copy 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 Code: Copy 'canvas1 defined in designer asJavaObject(canvas1).RunMethod("setWidth",array(100.00)) EventHandlers become easier too Code: Copy Dim myEventHandler As Object = asJavaObject(ob).CreateEventFromUI("javafx.event.EventHandler", _ handlerName,False) asJavaObject(ob).RunMethod(eventName, Array(myEventHandler)) Click to expand...
This is probably the shortest snippet ever, but I find it quite useful Code: Copy 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 Code: Copy 'canvas1 defined in designer asJavaObject(canvas1).RunMethod("setWidth",array(100.00)) EventHandlers become easier too Code: Copy Dim myEventHandler As Object = asJavaObject(ob).CreateEventFromUI("javafx.event.EventHandler", _ handlerName,False) asJavaObject(ob).RunMethod(eventName, Array(myEventHandler))
LucaMs Expert Licensed User Longtime User Sep 27, 2014 #2 Daestrum said: Code: Copy Sub asJavaObject(j As JavaObject) As JavaObject Return j End Sub Click to expand... ???
Daestrum said: Code: Copy Sub asJavaObject(j As JavaObject) As JavaObject Return j End Sub Click to expand... ???
derez Expert Licensed User Longtime User Sep 27, 2014 #3 Sub asJavaObject(j As Object) As JavaObject ?
Daestrum Expert Licensed User Longtime User Sep 27, 2014 #4 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 Code: Copy Dim jo as javaobject = anobject jo.runmethod(......) But in one line.
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 Code: Copy Dim jo as javaobject = anobject jo.runmethod(......) But in one line.
warwound Expert Licensed User Longtime User Sep 28, 2014 #5 The JavaObject is anonymous too, no references are ever made to it. Nice n clean . Martin.
Daestrum Expert Licensed User Longtime User Sep 28, 2014 #6 Eventhandler using the method, taken to extreme becomes Code: Copy 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: Sep 28, 2014
Eventhandler using the method, taken to extreme becomes Code: Copy 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
LucaMs Expert Licensed User Longtime User Sep 28, 2014 #7 Daestrum said: 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 Code: Copy Dim jo as javaobject = anobject jo.runmethod(......) But in one line. Click to expand... 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 said: 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 Code: Copy Dim jo as javaobject = anobject jo.runmethod(......) But in one line. Click to expand... 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 Sep 28, 2014 #8 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 Code: Copy Dim b As Button Dim jo As JavaObject = b ' jo is now effectively b jo.RunMethod(....) Whereas you can using this method just have Code: Copy Dim b As Button asJavaObject(b).RunMethod(......)
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 Code: Copy Dim b As Button Dim jo As JavaObject = b ' jo is now effectively b jo.RunMethod(....) Whereas you can using this method just have Code: Copy Dim b As Button asJavaObject(b).RunMethod(......)
Daestrum Expert Licensed User Longtime User Feb 4, 2015 #10 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 Code: Copy Sub inline as JavaObject Return Me End Sub Then all the calls to your in-line code become similar to Code: Copy #If JAVA public static String myMethod() { return "Hello B4J"; } #End If .... Dim s As String = inline.RunMethod("myMethod",Null) ....
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 Code: Copy Sub inline as JavaObject Return Me End Sub Then all the calls to your in-line code become similar to Code: Copy #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 Feb 10, 2015 #11 Been rewriting some code and noticed the code I had in post #6 can be rewritten far clearer Code: Copy 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 Code: Copy 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
Been rewriting some code and noticed the code I had in post #6 can be rewritten far clearer Code: Copy 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 Code: Copy 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 Jan 18, 2019 #12 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 Code: Copy Dim jo as JavaObject jo.InitializeStatic("javafx.scene.paint.Color") Dim myColor As Int = jo.GetField("AZURE") What you can do is Code: Copy 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.
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 Code: Copy Dim jo as JavaObject jo.InitializeStatic("javafx.scene.paint.Color") Dim myColor As Int = jo.GetField("AZURE") What you can do is Code: Copy 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.