B4J Question Problem : Build Button Array

I wrote the following code in B4J to create a loop to set the values of the buttons I created in the form, but unfortunately it does not work and returns an error. Please help.

Sub Process_Globals
Private MainForm As Form
Private Button1 As Button
Private Button2 As Button
Private Button3 As Button
Private Button4 As Button
Private Button5 As Button
Private Button6 As Button
Dim ls As List
End Sub

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

ls.Initialize
ls.AddAll(Array As Button(Button1,Button2,Button3,Button4,Button5,Button6))
End Sub

Sub Button1_Click
For t=0 To 5
Dim bt As Button=ls.Get(0)
bt.Text=""
Next
End Sub
 
I don't think there is an error but only the first button clear the Text.
Should have written ls.Get(t) if you want to clear all 6 buttons.
B4X: Copy
For i = 0 To 5
    Dim bt As Button = ls.Get(i)
    bt.Text = ""
Next
I'm sorry, but even with your method I get the following error:

Code: Copy
java.lang.ClassCastException: class anywheresoftware.b4j.objects.ButtonWrapper cannot be cast to class javafx.scene.control.Labeled (anywheresoftware.b4j.objects.ButtonWrapper is in unnamed module of loader 'app'; javafx.scene.control.Labeled is in module javafx.controls of loader 'app')
 
Upvote 0
Change:
Code: Copy
ls.AddAll(Array As Button(Button1,Button2,Button3,Button4,Button5,Button6))
To:
Code: Copy
ls.AddAll(Array(Button1,Button2,Button3,Button4,Button5,Button6))

List.AddAll expects an array of generic objects.
I never noticed this as I only use AddAll for Array As String(), it worked.
 
Upvote 0
B4X: Copy
ls.AddAll(Array As B4XView(Button1,Button2,Button3,Button4,Button5,Button6))

B4X: Copy
Private Sub Button1_Click
    Dim bt As B4XView = ls.Get(0)
    bt.Text = ""
End Sub

This work.
 
Upvote 0
Back
Top