B4J Question [B4X] [SOLVED] Create dynamically B4Xpanel

Magma

Well-Known Member
Licensed User
[B4X] Create dynamically B4Xpanel

Well..
I ve decided to use XUI at my projects after years... so i am confused about creating my pane on the fly adding into them textfields.. without using loadlayout...

So for example I want to create an array of 10 panel with 10 b4x edittext fields...

How will be that on the fly... (?)

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private xbase As B4XPanel   'at a layout...
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show

    Dim p(10) As B4XView = xui.CreatePanel("")
    p(1).SetLayoutAnimated(0, 0, 0,100, 50) 'is that really needed ?
    xbase.AddView(p(1), 0, 0,100,50)
'For example let's add one panel... but...

but...


B4X:
main._appstart (java line: 62)
java.lang.ClassCastException: class anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane cannot be cast to class [Lanywheresoftware.b4a.objects.B4XViewWrapper; (anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane and [Lanywheresoftware.b4a.objects.B4XViewWrapper; are in unnamed module of loader 'app')
    at b4j.example.main._appstart(main.java:62)
    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:108)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
    at b4j.example.main.start(main.java:37)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    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)
 

DonManfred

Expert
Licensed User
The CORRECT way is TO USE loadlayout!
 
Upvote 0

Magma

Well-Known Member
Licensed User
@Alexander Stolte --- I thought it for easy control... but from the other hand the example point me DonManfred helped me with the tag /// Thanks guys !
 
Upvote 0

Didier9

Well-Known Member
Licensed User
The CORRECT way is TO USE loadlayout!

What if you do not always want the same number of elements?

A method that exists for the purpose of adding screen elements to a panel programmatically is by definition not incorrect to use if it is defined.
There may be incorrect ways to implement it but using it cannot be incorrect if it is part of the toolbox.

I have an app where I can have 10 or 16 groups of elements. I cannot have a static layout, I must be able to change the screen layout and readjust spacing between elements depending on how many I need to display.
 
Upvote 0

Magma

Well-Known Member
Licensed User
1) What if you do not always want the same number of elements?

2) A method that exists for the purpose of adding screen elements to a panel programmatically is by definition not incorrect to use if it is defined.
There may be incorrect ways to implement it but using it cannot be incorrect if it is part of the toolbox.

3) I have an app where I can have 10 or 16 groups of elements. I cannot have a static layout, I must be able to change the screen layout and readjust spacing between elements depending on how many I need to display.

@Didier9
1) DonManfred not say that will be the same number of elements... check the link and you will understand...
2) The method for adding elements - yes is that i ve said... and sure you can find many ways to "talk" with them...
3) If there are many elements maybe use a scrollview...
 
Upvote 0

Didier9

Well-Known Member
Licensed User
The original question had to do with loading elements without using loadlayout. Don answered that using loadlayout was the CORRECT way, implying it was the only way.

What I meant is that using loadlayout may be preferred (Erel has been very clear about it) but in my opinion that is not always the case.

A correct way to do something is what results in efficient code that is maintainable and bug free. There may be more than one way to achieve that.

In my experience, using loadlayout is not always the best way as in it is not always the most efficient or easiest to maintain. Your mileage may vary.
 
Last edited:
Upvote 0

Magma

Well-Known Member
Licensed User
...Actually i asked about loading elements without loadlayout (but ofcourse i mean designed layout elements)... but also asked about b4xpanel...

XUI ... B4XView and B4XPanel seems like custom view....

until today i have to say that always creating with initializing and .addnode ...

but seems more easier for b4xpanels to achieved that - anyway your "words" are right and "clear" !
 
Upvote 0

Star-Dust

Expert
Licensed User
When you talk about b4xpanel perhaps you are referring to a class from the SD_XUIView library. If so then ask the question on the library thread

however i suspect you are using the library for b4a instead of the one for b4j
 
Last edited:
Upvote 0

Magma

Well-Known Member
Licensed User
Upvote 0

Star-Dust

Expert
Licensed User
This Is wrong
B4X:
Dim p(10) As B4XView = xui.CreatePanel("")
you cannot assign a single view to an Array of views

Change to
B4X:
Dim p(10) As B4XView
For i=0 to 9
    P(i)=xui.CreatePanel("")
Next
 
Upvote 0

Magma

Well-Known Member
Licensed User
@Star-Dust so you think will be easier to use array for creating dynamically-programmaticaly the b4xpanels or loadlayout like this... ?
(the reason will be using b4xpanels is the option that you have - movible / resizeable -) It is super!
 
Upvote 0

Star-Dust

Expert
Licensed User
I am not claiming that one way is simpler than another. simply that the way you wrote can't work if you don't loop.

If you want to dynamically create B4xPanel then in addition to the best method suggested by Erel you can use this
B4X:
    Dim Base As B4XView = MainForm.RootPane
    MainForm.RootPane.Style="-fx-background-color: gray"
    Dim Pnl(5) As B4XPanel
    For i=0 To 4
        Pnl(i).Initialize(Me,"Pnl")
        Pnl(i).AddToParent(Base,0,i*100,90,90)
        Pnl(i).GetBase.Style="-fx-background-color: white"
    Next
 
Last edited:
Upvote 0
Top