B4J Question filling table view columns dynamically

Nokia

Active Member
Licensed User
Longtime User
I am trying to fill a table view control with an unknown set of columns. for now I am testing with 10 columns. the only given is the first 3 columns, after that it will depend on values in a database. I plan on filling the data with 2 sql loops the first main loop will get the first 3 columns and the second loop will get the data for the unknown amount of columns (limited now to 7 records).

I do know the column total from a select statement that fills the columns successfully, now i am trying to fill the table with data. here is the code I have. the second loop is for testing to fill the rest of the columns with empty data for testing.

B4X:
       Dim L(tblTaskUsers.ColumnsCount) As Label       
       Do While rs.NextRow
           
           Dim i As Int = 0
           
           Dim LL As Label
           LL.Initialize("")
           L(i) = LL
           L(i + 1) = LL
           L(i + 2) = LL
           
           L(i).TextColor = fx.Colors.Black
           L(i + 1).TextColor = fx.Colors.Black
           L(i + 2).TextColor = fx.Colors.Black

           L(i).Text = rs.GetString("last_name") & ", " & rs.GetString("first_name") & " " & rs.GetString("mi")
           L(i + 1).Text = rs.GetString("Accepted")
           L(i + 2).Text = rs.GetString("Completed")

           i = i + 3
           For iI = 1 To tblTaskUsers.ColumnsCount - 3
               L(i) = LL
               L(i).TextColor = fx.Colors.Black
               L(i).Text = ""
               i = i + 1
           Next
           
           tblTaskUsers.Items.Add(Array As Object(L))
           
       Loop
       rs.Close

I am getting this error when I run it..


java.lang.ArrayIndexOutOfBoundsException: 1
at anywheresoftware.b4j.objects.TableViewWrapper$MyCellValueFactory.call(TableViewWrapper.java:211)
at anywheresoftware.b4j.objects.TableViewWrapper$MyCellValueFactory.call(TableViewWrapper.java:1)
at javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:578)
at javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:563)
at javafx.scene.control.TableCell.updateItem(TableCell.java:644)
at javafx.scene.control.TableCell.indexChanged(TableCell.java:468)
at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:116)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:533)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.init(TableRowSkinBase.java:147)
at com.sun.javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:64)
at javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:212)
at javafx.scene.control.Control.impl_processCSS(Control.java:872)
at javafx.scene.Node.processCSS(Node.java:9056)
at javafx.scene.Node.applyCss(Node.java:9153)
at com.sun.javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1964)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1797)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1879)
at com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2528)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1189)
at javafx.scene.Parent.layout(Parent.java:1087)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:354)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:381)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

any ideas?
 

Nokia

Active Member
Licensed User
Longtime User
You are using the same label for three different cells.

Add a sub that creates the cell's label:
B4X:
Sub CreateLabel(s As String) As Label
 Dim l As Label
 l.Initialize("")
 l.Text = s
 Return l
End Sub

L(i) = CreateLabel(...)
L(i + 1) = CreateLabel(...)


I tried it this way and still have same error..
B4X:
       Dim L(tblTaskUsers.ColumnsCount) As Label       
       Do While rs.NextRow
           
           Dim i As Int = 0

           L(i) = Createlabel(rs.GetString("last_name") & ", " & rs.GetString("first_name") & " " & rs.GetString("mi"))
           L(i + 1) = Createlabel(rs.GetString("Accepted"))
           L(i + 2) = Createlabel(rs.GetString("Completed"))

           i = i + 3
           For iI = 1 To tblTaskUsers.ColumnsCount - 3
               L(i) = Createlabel(i)
               i = i + 1
           Next
           
           tblTaskUsers.Items.Add(Array As Object(L))

       Loop
       rs.Close
       'sqlMS.TransactionSuccessful
   Else
       Log(LastException)
       mdlShared.LogErrorToFile("sqlLoadUserTask_Ready.1", LastException.Message)
       'sqlMS.Rollback
   End If
   '--------------------------------------------------------------------------------------------
End Sub

Sub Createlabel( sText As String) As Label
   Dim l As Label
   l.Initialize("")
   l.Text = sText
   Return l
End Sub

Error:

java.lang.ArrayIndexOutOfBoundsException: 1
at anywheresoftware.b4j.objects.TableViewWrapper$MyCellValueFactory.call(TableViewWrapper.java:211)
at anywheresoftware.b4j.objects.TableViewWrapper$MyCellValueFactory.call(TableViewWrapper.java:1)
at javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:578)
at javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:563)
at javafx.scene.control.TableCell.updateItem(TableCell.java:644)
at javafx.scene.control.TableCell.indexChanged(TableCell.java:468)
at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:116)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:533)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.init(TableRowSkinBase.java:147)
at com.sun.javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:64)
at javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:212)
at javafx.scene.control.Control.impl_processCSS(Control.java:872)
at javafx.scene.Node.processCSS(Node.java:9056)
at javafx.scene.Node.applyCss(Node.java:9153)
at com.sun.javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1964)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1797)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1879)
at com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2528)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1189)
at javafx.scene.Parent.layout(Parent.java:1087)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:354)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:381)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
java.lang.ArrayIndexOutOfBoundsException: 1
at anywheresoftware.b4j.objects.TableViewWrapper$MyCellValueFactory.call(TableViewWrapper.java:211)
at anywheresoftware.b4j.objects.TableViewWrapper$MyCellValueFactory.call(TableViewWrapper.java:1)
at javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:578)
at javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:563)
at javafx.scene.control.TableCell.updateItem(TableCell.java:644)
at javafx.scene.control.TableCell.indexChanged(TableCell.java:468)
at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:116)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:533)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.init(TableRowSkinBase.java:147)
at com.sun.javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:64)
at javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:212)
at javafx.scene.control.Control.impl_processCSS(Control.java:872)
at javafx.scene.Parent.impl_processCSS(Parent.java:1280)
at javafx.scene.Parent.impl_processCSS(Parent.java:1280)
at javafx.scene.Parent.impl_processCSS(Parent.java:1280)
at javafx.scene.control.Control.impl_processCSS(Control.java:868)
at javafx.scene.Parent.impl_processCSS(Parent.java:1280)
at javafx.scene.Parent.impl_processCSS(Parent.java:1280)
at javafx.scene.Parent.impl_processCSS(Parent.java:1280)
at javafx.scene.control.Control.impl_processCSS(Control.java:868)
at javafx.scene.Node.processCSS(Node.java:9056)
at javafx.scene.Node.processCSS(Node.java:9049)
at javafx.scene.Node.processCSS(Node.java:9049)
at javafx.scene.Scene.doCSSPass(Scene.java:545)
at javafx.scene.Scene.access$3600(Scene.java:159)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2392)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:354)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:381)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

as far as I can tell.. I am only loading 10 columns and only 10 get loaded get created before this code executes.. "tblTaskUsers.Items.Add(Array As Object(L))"
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
You are using the same label for three different cells.

Add a sub that creates the cell's label:
B4X:
Sub CreateLabel(s As String) As Label
 Dim l As Label
 l.Initialize("")
 l.Text = s
 Return l
End Sub

L(i) = CreateLabel(...)
L(i + 1) = CreateLabel(...)


something else I have noticed.. when I use this line..
B4X:
tblTaskUsers.Items.AddAll(Array As Object(L))

all the data fills the table and all column data show as the label object, but in has repeated the last line of data is the rs the amount of records they are. when I log the data it shows correctly in the readout.. hmmm
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
What does tblTaskUsers.ColumnsCount return as the column count ?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You probably should have this line
B4X:
Dim L(tblTaskUsers.ColumnsCount) As Label
inside the Do While loop, before the
B4X:
Dim i as Int = 0

You should also change the line
B4X:
Dim L(tblTaskUsers.ColumnsCount) As Label
to read
B4X:
Dim L(tblTaskUsers.ColumnsCount) As Object

And change
B4X:
tblTaskUsers.Items.Add(Array As Object(L))
to
B4X:
tblTaskUsers.Items.Add(L)
 
Last edited:
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
You probably should have this line
B4X:
Dim L(tblTaskUsers.ColumnsCount) As Label
inside the Do While loop, before the
B4X:
Dim i as Int = 0

You should also change the line
B4X:
Dim L(tblTaskUsers.ColumnsCount) As Label
to read
B4X:
Dim L(tblTaskUsers.ColumnsCount) As Object

And change
B4X:
tblTaskUsers.Items.Add(Array As Object(L))
to
B4X:
tblTaskUsers.Items.Add(L)

changing from Label to Object did the trick.. thanks.. Daestrum!!!!
 
Upvote 0
Top