B4J Code Snippet Create objects in-line

This again may or may not be useful to others, but I thought I would share.

Basically them main part is the 'create' code module, which can be bolted on to any project. It allows you to create objects in-line.
As it stands it is not an exhaustive list of controls, just ones I have been playing around with.

The create code module - just edit to your needs - note I have not tested all on the list - I just grabbed the names from the javadoc for javafx.scene.control.??

B4X:
'Static code module
Private Sub Process_Globals
    Private fx As JFX
    Private mInstance As Map
    Private mStatic As Map
End Sub
Sub init
    mInstance.Initialize
    mStatic.Initialize
    mInstance.Put("Button","javafx.scene.control.Button")
    mInstance.Put("Slider","javafx.scene.control.Slider")
    mInstance.Put("Accordion","javafx.scene.control.Accordion")
    mInstance.Put("Cell","javafx.scene.control.Cell")
    mInstance.Put("ChoiceBox","javafx.scene.control.ChoiceBox")
    mInstance.Put("ColorPicker","javafx.scene.control.ColorPicker")
    mInstance.Put("CustomMenuItem","javafx.scene.control.CustomMenuItem")
    mInstance.Put("DateCell","javafx.scene.control.DateCell")
    mInstance.Put("DatePicker","javafx.scene.control.DatePicker")
    mInstance.Put("FocusModel","javafx.scene.control.FocusModel")
    mInstance.Put("Hyperlink","javafx.scene.control.Hyperlink")
    mInstance.Put("IndexedCell","javafx.scene.control.IndexedCell")
    mInstance.Put("IndexRange","javafx.scene.control.IndexRange")
    mInstance.Put("Labeled","javafx.scene.control.Labeled")
    mInstance.Put("ListCell","javafx.scene.control.ListCell")
    mInstance.Put("MenuButton","javafx.scene.control.MenuButton")
    mInstance.Put("PasswordField","javafx.scene.control.PasswordField")
    mInstance.Put("PopupControl","javafx.scene.control.PopupControl")
    mInstance.Put("RadioMenuItem","javafx.scene.control.RadioMenuItem")
    mInstance.Put("ScrollBar","javafx.scene.control.ScrollBar")
    mInstance.Put("Separator","javafx.scene.control.Separator")
    mInstance.Put("SeparatorMenuItem","javafx.scene.control.SeparatorMenuItem")
    mInstance.Put("SplitMenuButton","javafx.scene.control.SplitMenuButton")
    mInstance.Put("SplitPane","javafx.scene.control.SplitPane")
    mInstance.Put("SplitPane.Divider","javafx.scene.control.SplitPane.Divider")
    mInstance.Put("Tab","javafx.scene.control.Tab")
    mInstance.Put("TableCell","javafx.scene.control.TableCell")
    mInstance.Put("TableColumn","javafx.scene.control.TableColumn")
    mInstance.Put("TableRow","javafx.scene.control.TableRow")
    mInstance.Put("TextInputControl","javafx.scene.control.TextInputControl")
    mInstance.Put("TitledPane","javafx.scene.control.TitledPane")
    mInstance.Put("ToggleGroup","javafx.scene.control.ToggleGroup")
    mInstance.Put("ToolBar","javafx.scene.control.ToolBar")
    mInstance.Put("Tooltip","javafx.scene.control.Tooltip")
End Sub
Sub Instance(ss As String,parms() As Object) As JavaObject
    Dim jo As JavaObject
    Try
    Return jo.InitializeNewInstance(mInstance.Get(ss),parms)
    Catch
        Log("Error : "& LastException.Message)
    End Try  
End Sub

Now the fun part in your project :)
Using the above it allows for the following code. (I use java 8_20 and B4J v2.20, so all may not work if you are not on the same versions)
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private jo As JavaObject
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    create.init
    jo = create.Instance("Accordion",Null)
    jo.RunMethodJO("getPanes",Null).RunMethod("add",Array(create.Instance("TitledPane",Array("button",create.Instance("Button",Array("b"))))))
    jo.RunMethodJO("getPanes",Null).RunMethod("add",Array(create.Instance("TitledPane",Array("button 1",create.Instance("Button",Array("b1"))))))
    jo.RunMethodJO("getPanes",Null).RunMethod("add",Array(create.Instance("TitledPane",Array("button 2",create.Instance("Button",Array("b2"))))))
    Log(jo)
    MainForm.RootPane.AddNode(jo,10,10,250,30)
End Sub
(Yes I know it makes the program look like LISP with all the parenthesis)

An explanation of the above code:
1, create an Instance of an Accordian
B4X:
jo.create.Instance("Accordian",null)
2, get the panes of the Accordian, add an inline created TitledPane and add to that an inline created button.
B4X:
   jo.RunMethodJO("getPanes",Null).RunMethod("add",Array(create.Instance("TitledPane",Array("button",create.Instance("Button",Array("b"))))))

3, add more TitledPanes with Buttons, and finally add to rootPane.

Complicated I know if you are not familiar with Java's ability to create things as needed using 'new' keyword.
 
Last edited:

ivanomonti

Expert
Licensed User
Longtime User
B4X:
Dim txx As JavaObject
txx = Mod_Code.InstanceJavaObject("PasswordField",Null)
txx.text = "Pasword" ??? <<<<<<<<< possible
AccountForm.RootPane.AddNode(txx,xl,xt,xw,xh)


B4X:
Sub login_MouseClicked (EventData As MouseEvent)
    Mod_Code.messagebox(txx.text) <<<<<<<<< possible
End Sub

thank
 

Daestrum

Expert
Licensed User
Longtime User
I have not tried it , but I think you would need to use the setText and getText methods (or equivalent for the control)

B4X:
...
txx.RunMethod("setText",array("password")
...
 

BeneBarros

Active Member
Licensed User
Longtime User
excuse my lack of knowledge, but, as I could access the object's events?
_MouseClicked, _FocusChanged, Etc ..
 

Daestrum

Expert
Licensed User
Longtime User
You could set the handlers for the events ie setOnMouseClicked, but you would still need to either use Ba raiseevent or some java code to get it to refer to your B4J subs.
 
Top