B4J Question how to justify numbers to right on tableview

raphipps2002

Active Member
Licensed User
Longtime User
I have a simple tableview which loads mysql data into it. I number columns justified to the right want cant see any property in the scene builder or indeed on the column properties?

How is this possible?

THanks
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
it is a good question, the best answer going around the forum is this piece of code:


this work with labels inside the tableview, but with a few tweaks there and there you can add checkboxes and more views.
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) = WrapView(Clabel,alignments(col))
        Next
        TableView1.Items.Add(values)
        If Limit > 0 And TableView1.Items.Size >= Limit Then Exit
    Loop
    cur.Close
End Sub

Public Sub WrapView(lbl As Object, 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

you can use it like this:

B4X:
executetableview2(sqlite,"Select * FROM TABLE",null,0,tableview1,array as string("CENTER","CENTER-LEFT"))
 
Last edited:
Upvote 0

raphipps2002

Active Member
Licensed User
Longtime User
Oh ...wow...that's complicated :)....I got it to work so thanks but its a pity Scene Builder just doesn't have a property justify.left or right!

much obliged for your help!
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
You are welcome!

To extract the Text from the tableview you have to write something like this:

B4X:
dim row() as object = tableview.selectedindex
dim P as pane = row(0) ' the index where you want to extract the info
dim L as label = P.getnode(0)
log(L.text)

that code was wrriten by Erel and modified by me, you can get more DB stuff with Dbutils class:
https://www.b4x.com/android/forum/threads/dbutils-example.34611/
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
If you are using java 8 you can use
B4X:
Dim jo As JavaObject = tv
jo.RunMethodJO("getColumns",Null).RunMethodJO("get",Array(1)).RunMethod("setStyle",Array("-fx-alignment: CENTER-RIGHT;"))

The '1' in the array after the "get" is the column number you want to justify (zero based)
 
Upvote 0
Top