B4J Question [SOLVED] Dynamically fill TableView with columns and data from SQL script

Nokia

Active Member
Licensed User
Longtime User
So what I am trying to do is fill a table view from a script someone pasts into a Text Area. I am able to get the columns to show but when I try to add data I get an error:

java.lang.ClassCastException: javafx.scene.control.Label cannot be cast to [Ljava.lang.Object;
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:9063)
at javafx.scene.Node.applyCss(Node.java:9160)
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$2(Toolkit.java:398)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:561)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:541)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:534)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:340)
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$4(WinApplication.java:186)
at java.lang.Thread.run(Thread.java:748)

I am able to see that data is being added to the list but when I try at add list with tblScript.Items.AddAll(ColumnData), I seem to get the error.

My code below:
B4X:
        'need to get columnlist
        Dim Column As List
        Column.Initialize
        For i = 0 To rs.ColumnCount - 1
            Column.Add(rs.GetColumnName(i))
        Next
        'setting columns
        tblScript.SetColumns(Column)
                
        Do While rs.NextRow
        
            Try
                Dim ColumnData As List
                ColumnData.Initialize
                
                Log("Column Count:" & Column.Size)
                
                For i = 0 To Column.Size - 1
                                    
                    Log(Column.Get(i))
                    Log(CheckNull(rs.GetString(Column.Get(i))))
                    
                    ColumnData.Add(CheckNull(rs.GetString(Column.Get(i))))
                    
                Next
                tblScript.Items.AddAll(ColumnData)
            Catch
                Log(LastException)
            End Try
            Sleep(0)
        Loop
        rs.Close
B4X:
Sub CheckNull(Value As Object) As String
    
    Dim s As String = " "
    If Value = Null Then
        s = " "
    Else
        s = Value
    End If
    
    Return s
End Sub

Any Ideas what I am doing wrong?

also is anybody has any recomdnedation on setting column width automatically. drop a line..
 

Nokia

Active Member
Licensed User
Longtime User
so I see where I went wrong.. I was using a list instead of an array. Data now loads and I also solved the auto field length issue as well..

B4X:
        'need to get columnlist
        Dim Column As List
        Column.Initialize
        For i = 0 To rs.ColumnCount - 1
            Column.Add(rs.GetColumnName(i))
        Next
        'setting columns
        tblScript.SetColumns(Column)
        For i = 0 To tblScript.ColumnsCount - 1
            tblScript.SetColumnWidth(i,(tblScript.GetColumnHeader(i).Length * 10) + 10)
        Next
                
        Do While rs.NextRow
            Try               
                Dim ColumnDataA(tblScript.ColumnsCount) As Object
                For i = 0 To tblScript.ColumnsCount - 1

                    Dim dv As String = CheckNull(rs.GetString2(i))
                    Dim cw As Int = (dv.Length* 10) + 10
                    ColumnDataA(i) = dv
                    If cw >= tblScript.GetColumnWidth(i) Then
                        tblScript.SetColumnWidth(i,cw)
                    End If
                Next
                tblScript.Items.Add(ColumnDataA)
            Catch
                Log(LastException)
                mdlShared.LogErrorToFile("MainForm.sqlScriptLoad_Ready.1", LastException.Message)
            End Try
            Sleep(0)
        Loop
        rs.Close
 
Upvote 0
Top