B4J Question Tableview Set cell alignment

tdocs2

Well-Known Member
Licensed User
Longtime User
Greetings.

I have looked thru the documentation with great detail, and found to my surprise that there is no Tableview Set cell alignment.

I have seen this post, but it seems quite archaic. I added my tableview with the Open Designer (GREAT TOOL EREL :):):)). I filled it with DBUtils (GREAT TOOL EREL :):):)), but I cannot set cell alignment.

Have I missed something? Is there a simple way to do this?

Thank you in advance for your replies.

Sandy
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You have two options to change a cell value:
1. Get the list of rows with TableView.Items. Modify the relevant cell and set the items again. The downsize is that the table position will be lost.
2. Instead of adding strings as items you can add Labels. Then you can get the relevant label and change its Text directly.
 
Upvote 0

tdocs2

Well-Known Member
Licensed User
Longtime User
Thank you, Erel.

2. Instead of adding strings as items you can add Labels. Then you can get the relevant label and change its Text directly.

I added my tableview with the Open Designer. I filled the Tableview with DBUtils ExecuteTableView which adds strings. Truly EXCELLENT - RAD at its best.

I am sorry, but I am lost on your solution... Should there be an ExecuteTableView2 in DBUtils which adds labels instead of values so that I can set the alignment?

Thank you for your patience and tolerance...

Sandy
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

adding a centered label to a TableView Row requires additional coding.
This sub can be used as one example on how to do so (will need JavaObject library).

Example adding two labels (included in a Pane) to the TableView Row - To note that the Label properties can be set (in this text color blue).
B4X:
    TableView1.Items.Add(Array As Object(TableView_AddLabel("Label 1", "CENTER"), TableView_AddLabel("Label 2", "CENTER")))

B4X:
Sub TableView_AddLabel(labeltext As String, Alignment As String) As Pane
    Dim lbl1 As Label
    lbl1.Initialize("")
    lbl1.text = labeltext
    lbl1.TextColor = fx.Colors.Blue
    Dim pn1 As AnchorPane
    pn1.Initialize("")
    pn1.AddNode(lbl1, 0, 0, -1, -1)
    pn1.FillHorizontally(lbl1, 0, 0)
    Dim jo1 = lbl1 As JavaObject
    jo1.RunMethod("setAlignment", Array As Object(Alignment))
    Return pn1
End Sub

To get the content of a cell, you will need following code - because each cell content is a Pane with a Label (or in other words, the Pane has one Node being a Label):
B4X:
Sub TableView1_SelectedCellChanged (RowIndex As Int, ColIndex As Int, Cell As Object)
   Dim p As Pane = Cell
   Dim l As Label = p.GetNode(0)
   Log(l.Text)   
End Sub
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
in DBUTILS there is not a executetableview2, i created one that center all columns, shouldnt be hard to specify more alignments.

Copy this code to the DBUTILS Class:

B4X:
Public Sub ExecuteTableView2(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, _
    TableView1 As TableView)
    TableView1.Items.Clear
    Dim cur As ResultSet
    If StringArgs = Null Then 
        Dim StringArgs(0) As String
    End If
    cur = SQL.ExecQuery2(Query, StringArgs)
    Dim cols As List
    cols.Initialize
    For i = 0 To cur.ColumnCount - 1
        cols.Add(cur.GetColumnName(i))
    Next
    TableView1.SetColumns(cols)
    Do While cur.NextRow
        Dim values(cur.ColumnCount) As Object
        For col = 0 To cur.ColumnCount - 1
            Dim Clabel As Label
            Clabel.Initialize("")
            Clabel.Text = cur.GetString2(col)
            values(col) = WrapLabel(Clabel,"CENTER")
        Next
        TableView1.Items.Add(values)
        If Limit > 0 And TableView1.Items.Size >= Limit Then Exit
    Loop
    cur.Close
End Sub

Sub WrapLabel(lbl As Label, Alignment As String) As Pane
   Dim pn1 As AnchorPane
   pn1.Initialize("")
   pn1.AddNode(lbl, 0, 0, -1, -1)
   pn1.FillHorizontally(lbl, 0, 0)
   Dim jo1 = lbl As JavaObject
   jo1.RunMethod("setAlignment", Array As Object(Alignment))
   Return pn1
End Sub
 
Upvote 0

tdocs2

Well-Known Member
Licensed User
Longtime User
Enrique,

Thank you - :):):). You are brilliant.

Development of the ExecuteTableView2 sub you developed is above my tech skills.

At the risk of prevailing on your generosity, may I make a WISH?

Could you add a parameter to the sub ExecuteTableView2 that indicates the alignment for each column?
For example, if there are four columns, an array is passed with 4 strings that would indicate LEFT, CENTER, OR RIGHT for each column.

Then, instead of defaulting to CENTER as it does now, the sub would set the alignment indicated for the column.

Thank you for your generosity.

Sandy
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
of course Tdocs2

here it is the updated version:

B4X:
Public Sub ExecuteTableView2(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, _
    TableView1 As TableView, alignments() As String)
    TableView1.Items.Clear
    Dim cur As ResultSet
    If StringArgs = Null Then
        Dim StringArgs(0) As String
    End If
    cur = SQL.ExecQuery2(Query, StringArgs)
    Dim cols As List
    cols.Initialize
    For i = 0 To cur.ColumnCount - 1
        cols.Add(cur.GetColumnName(i))
    Next
    TableView1.SetColumns(cols)
    Do While cur.NextRow
        Dim values(cur.ColumnCount) As Object
        For col = 0 To cur.ColumnCount - 1
            Dim Clabel As Label
            Clabel.Initialize("")
            Clabel.Text = cur.GetString2(col)
            values(col) = WrapLabel(Clabel,alignments(col))
        Next
        TableView1.Items.Add(values)
        If Limit > 0 And TableView1.Items.Size >= Limit Then Exit
    Loop
    cur.Close
End Sub

and use it like this:

B4X:
DBUtils.ExecuteTableView2(sqlite,"SELECT * FROM TABLE",Null,0,TableView1,Array As String("CENTER","CENTER_RIGHT"))

from the original executetableview i just changed like 5 words, the original creator is the one that blows minds.
 
Upvote 0

tdocs2

Well-Known Member
Licensed User
Longtime User
To get the content of a cell, you will need following code - because each cell content is a Pane with a Label (or in other words, the Pane has one Node being a Label):
B4X:
Sub TableView1_SelectedCellChanged (RowIndex As Int, ColIndex As Int, Cell As Object)
   Dim p As Pane = Cell
   Dim l As Label = p.GetNode(0)
   Log(l.Text)  
End Sub

Thank you, Rob.

Your numerous contributions on your site are appreciated by all. Thank you for your generosity.

Sandy
 
Upvote 0

tdocs2

Well-Known Member
Licensed User
Longtime User
Thank you, rboeck.

To keep the documentation on this thread...

B4X:
Enum Constants

 


Enum Constant and Description

BASELINE_CENTER
Represents positioning on the baseline vertically and on the center horizontally.
BASELINE_LEFT
Represents positioning on the baseline vertically and on the left horizontally.
BASELINE_RIGHT
Represents positioning on the baseline vertically and on the right horizontally.
BOTTOM_CENTER
Represents positioning on the bottom vertically and on the center horizontally.
BOTTOM_LEFT
Represents positioning on the bottom vertically and on the left horizontally.
BOTTOM_RIGHT
Represents positioning on the bottom vertically and on the right horizontally.
CENTER
Represents positioning on the center both vertically and horizontally.
CENTER_LEFT
Represents positioning on the center vertically and on the left horizontally.
CENTER_RIGHT
Represents positioning on the center vertically and on the right horizontally.
TOP_CENTER
Represents positioning on the top vertically and on the center horizontally.
TOP_LEFT
Represents positioning on the top vertically and on the left horizontally.
TOP_RIGHT
Represents positioning on the top vertically and on the right horizontally.
 
Upvote 0

Otto Tentacle

New Member
Hi thanks for the ExecuteTableView2 code it works well, but, how do I get data from the selected row?

For instance I fill a 4 column tableview with data from an SQLite table, when I select a row in the tableview I would like to get the value in the first column (the ID field of the SQLite table) and use it as a parameter in a query.

For ExecuteTableView I can just use the SelectedRowChanged action and Row(0) gives me a string with the required value e.g 12

For ExecuteTableView2 Row(0) now gives a random value along the lines of AnchorPane@3bdae5bb

Thanks
 
Upvote 0

Otto Tentacle

New Member
Thanks for the quick answer, the following works fine.

Sub TableView1_SelectedRowChanged(Index As Int, Row() As Object)
Dim p As Pane = Row(0)
Dim lbl As Label = p.GetNode(0)
Log(lbl.text)
End Sub
 
Upvote 0
Top