B4J Question Load Layout from a different folder

Cableguy

Expert
Licensed User
Longtime User
Hi Guys

I need to load a layout from a file other than the assets folder, but the loadlayout method does not allow it.
Is there any other way to accomplish this, maybe using JObject or even inline Java?
 

Roycefer

Well-Known Member
Licensed User
Longtime User
B4J's PaneWrapper.LoadLayout method and B4J's FXMLBuilder.LoadLayout method both expect the layout file to be in the assets folder. I'm afraid you'll have to implement something yourself using javafx.fxml.FXMLLoader.

Check out this tutorial: http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm . Using Example 4-1 as inspiration, the Scene object should come from the Form into which you are trying to load the layout. It will be a public member of the Form of type javafx.scene.Scene called "scene". You can access it using JavaObject or Reflection or just pass the Form to your inline Java method and get at the "scene" member inside there. You can probably implement this whole thing using inline Java and Example 4-1 as a starting point.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
try
B4X:
...
Dim loader As JavaObject
Dim url As JavaObject
Dim myLayout As JavaObject
   loader.InitializeNewInstance("javafx.fxml.FXMLLoader",Null)
   url = asJO(Me).RunMethod("getURL",Array As String("file:///c:/b4j source/Files/filelist.fxml"))
   loader.RunMethod("setLocation",Array(url))
   myLayout = loader.RunMethod("load",Array(url))
   MainForm.WindowHeight = myLayout.RunMethod("getHeight",Null)
   MainForm.WindowWidth = myLayout.RunMethod("getWidth",Null)
   MainForm.RootPane.AddNode(myLayout,0,0,-1,-1)
   MainForm.Show
End Sub

Sub asJO(o As JavaObject) As JavaObject
   Return o
End Sub

#if java
import java.net.URL;
public static URL getURL(String s){
   URL myURL = null;
   try{
      myURL = new URL(s);
    } catch (Exception e) {
         System.out.println(e);
    }
    return myURL;
}
#end if
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
This is for a language tool, the screen doesn't need functionality, just needs to be loaded to work on it.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
@Daestrum , thanks for the snippet, I will try to Integrate it tonight.
@Erel, as daestrum stated, we don't need functionally, i.e. buttons click events, we only need the graphical aspect of the layout, and the name, text and I'd fields access.
 
Upvote 0
Top