B4J Question TableView with database column name + SetColumnStyle

vnpython

Member
Hi all seniors, after trying and playing (for learning purposes) with TableView tutorial, I managed to use the PostgreSQL database to populate the TableView but I failed trying to use the column name from "Select * from table" to populate the "SetColumns" and with the sample files from the TableView Tutorial, I also failed to get the SetColumnStyle(1, "-fx-alignment: CENTER;") to work on my code. Hoping I would seek some guidance over here. ps: #AdditionalJar: postgresql-42.6.0.jar is added to Main.

B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private pgSQL As SQL
    Private TableViewHD As TableView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    TableViewHD.SetColumns(Array As String("prod_line_id", "prod_line", "prod_line_desc"))
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    
    Try
        Dim connectionString As String = "jdbc:postgresql://localhost:5432/my_hd?user=postgres&password=Demo1234"
        pgSQL.Initialize("org.postgresql.Driver", connectionString)
        'Log("Connection successful!")
        Dim rs As ResultSet = pgSQL.ExecQuery("SELECT * FROM product_line")
        Do While rs.NextRow
            Dim values(rs.ColumnCount) As Object
            For i = 0 To rs.ColumnCount - 1
                values(i) = rs.GetString2(i)
            Next

            TableViewHD.Items.Add(values)

        Loop
        rs.Close
        pgSQL.Close
    Catch
        Log("Error: " & LastException.Message)
    End Try
    pgSQL.Close
End Sub

1691828484281.png
1691828516577.png
 

walt61

Active Member
Licensed User
Longtime User
Hi @vnpython , a partial answer regarding the columns and their headers (as I knew this by heart and can't experiment with the css right now):
B4X:
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    'TableViewHD.SetColumns(Array As String("prod_line_id", "prod_line", "prod_line_desc")) <----- TableViewHD.SetColumns is moved to Button1_Click
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    
    Try
        Dim connectionString As String = "jdbc:postgresql://localhost:5432/my_hd?user=postgres&password=Demo1234"
        pgSQL.Initialize("org.postgresql.Driver", connectionString)
        'Log("Connection successful!")
        Dim rs As ResultSet = pgSQL.ExecQuery("SELECT * FROM product_line")
        ' Setup the TableView columns
        Dim rsColumns As List
        rsColumns.Initialize
        For i = 0 To rs.ColumnCount - 1
            rsColumns.Add(rs.GetColumnName(i))
        Next
        TableViewHD.SetColumns(rsColumns)
        ' Process the data
        Do While rs.NextRow
            Dim values(rs.ColumnCount) As Object
            For i = 0 To rs.ColumnCount - 1
                values(i) = rs.GetString2(i)
            Next

            TableViewHD.Items.Add(values)

        Loop
        rs.Close
        pgSQL.Close
    Catch
        Log("Error: " & LastException.Message)
    End Try
    pgSQL.Close
End Sub
 
Upvote 0

vnpython

Member
@walt61 your code works perfectly! and now wait for other senior to share/teach the SetColumnStyle(1, "-fx-alignment: CENTER;") method to have the table view auto-align upon resizing of window.
 
Upvote 0
Top