Android Question B4XPages and #IF JAVA

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello,

I have a B4XPages code that is using specific java routines only for B4J. If compiled for B4A the routine raises an error that's why it's needed to restrict the compilation only to B4J.
But compiler in B4A is ignoring the directive as follows:
B4X:
#if B4J
#if Java
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javafx.scene.text.Font;
import javafx.scene.text.TextBoundsType;
public static double MeasureMultilineTextHeight(Font f, String text, double width) throws Exception {
  Method m = Class.forName("com.sun.javafx.scene.control.skin.Utils").getDeclaredMethod("computeTextHeight",
  Font.class, String.class, double.class, TextBoundsType.class);
  m.setAccessible(true);
  return (Double)m.invoke(null, f, text, width, TextBoundsType.LOGICAL_VERTICAL_CENTER);
  }
#End If
#END IF

Is there any way to reference a java function only for B4J?

Thanks!
 

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Don't be afraid to use conditional compilation:
B4X:
Private Sub MeasureTextHeight (...)
#If B4A
Dim su As StringUtils
...
#Else If B4J
Dim jo As JavaObject = Me
Return jo.RunMethod("MeasureMultilineTextHeight", Array(...))
#End If
You need to remove the 'static' modifier in the java code.
Thanks @Erel . I'm translating our apps to B4XPages and must to say that it's sensacional. The learning curve for B4X programmers like me is very fast and the result is incredible . Code reuse is about 100%.
Thanks for this great library.
 
Upvote 0
Top