B4J Code Snippet Metro Style with JMetro

1. Download jmetro-4.0.jar and copy it to the additional libraries folder.
https://github.com/JFXtras/jfxtras-styles/releases

2. Add reference to the jar:
B4X:
#AdditionalJar: jmetro-4.0

3. Apply the theme:
B4X:
Dim jo As JavaObject
jo.InitializeNewInstance("jfxtras.styles.jmetro8.JMetro", Array("LIGHT")) 'or DARK
Dim jform As JavaObject = MainForm
jo.RunMethod("applyTheme", Array(jform.GetField("scene")))

Before

SS-2018-08-09_15.04.17.png


After

SS-2018-08-09_15.04.51.png


Make sure to test it carefully as the theme changes some of the style attributes that are set by the designer.
 

ivanomonti

Expert
Licensed User
Longtime User
I'm trying version 11.6.9 and it gives me error in the string applyTheme as method not found, I was wondering if this updated version you could use.
 

androh

Member
Licensed User
Longtime User
Updated code for new jMetro version:

B4X:
#AdditionalJar: jmetro-11.6.14.jar

B4X:
Dim jo As JavaObject
Dim jform As JavaObject = MainForm
jo.InitializeNewInstance("jfxtras.styles.jmetro.JMetro", Array(jform.GetField("scene"), "LIGHT")) 'or DARK
jo.RunMethod("reApplyTheme", Null)
 

Rubsanpe

Active Member
Licensed User
Hi, When I try to use this code in a B4XPage project I get an error.

B4X:
Waiting for debugger to connect...
Program started.
*** mainpage: B4XPage_Appear
*** mainpage: B4XPage_Resize [mainpage]
*** b4xpage_rss: B4XPage_Created [mainpage]
Error occurred on line: 70 (B4XPage_RSS)
java.lang.reflect.InvocationTargetException
    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:566)
    at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:487)
    at b4j.example.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:878)
    at b4j.example.b4xpagesmanager._showpage(b4xpagesmanager.java:357)
    at b4j.example.b4xpages._showpage(b4xpages.java:71)
    at b4j.example.b4xmainpage._rss_click(b4xmainpage.java:184)
    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:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    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:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA$1.run(BA.java:236)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    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:834)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:134)
    at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:81)
    ... 30 more
Caused by: java.lang.reflect.InvocationTargetException
    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:566)
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:115)
    ... 31 more
Caused by: java.lang.RuntimeException: Field: scene not found in: javafx.scene.layout.AnchorPane
    at anywheresoftware.b4j.object.JavaObject$FieldCache.getField(JavaObject.java:307)
    at anywheresoftware.b4j.object.JavaObject.GetField(JavaObject.java:182)
    at b4j.example.b4xpage_rss._b4xpage_created(b4xpage_rss.java:161)
    ... 36 more

The same error occurs with the other foron examples when I try to use it in B4XPages. Any solutions or examples that work?

Thank you

Rubén
 
Top