B4J Question B4XTable Column Horizontal Alignment

rwblinn

Well-Known Member
Licensed User
Longtime User
In a B4XTable, how to horizontal align a column, i.e. "LEFT", "CENTER", "RIGHT" or "CENTER_LEFT"?

Tried below snippet, but gives a runtime error
B4X:
'Add column and get as object
Dim TypeColumn As B4XTableColumn = B4XTableDatapoints.AddColumn("Type", B4XTableDatapoints.COLUMN_TYPE_TEXT)
'Set the column width
TypeColumn.Width = 250dip
'Get the label from the column
Dim TypeLabel As B4XView = TypeColumn.Panel.GetView(TypeColumn.LabelIndex)
'Set text alignment
TypeLabel.SetTextAlignment("CENTER", "LEFT")

B4X:
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: Type does not match (class anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane)
 

behnam_tr

Active Member
Licensed User
Longtime User
B4X:
Sub SetColumnAlignment(tableName As B4XTable, columnID As String, alignment As String)
 
    Dim column As B4XTableColumn = tableName.GetColumn(columnID)
    For i = 1 To column.CellsLayouts.Size - 1        'starts at 1 due to header
        Dim pnl As B4XView = column.CellsLayouts.Get(i)
        pnl.GetView(0).SetTextAlignment("CENTER", alignment.ToUpperCase)
    Next
    
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Step 1: set the maximum number of rows and call BuildLayotusCache. This is very important as it saves you the trouble of dealing with more rows in the future.
Step 2: disable search highlight in B4J. The alignment of the highlighted cells will always be center in B4J.
Step 3:
B4X:
B4XTable1.MaximumRowsPerPage = 20
B4XTable1.BuildLayoutsCache(B4XTable1.MaximumRowsPerPage)
If xui.IsB4J Then B4XTable1.HighlightSearchResults = False
'Now you can call:
SetColumnHorizontalAlignment(NameColumn, "RIGHT")


Private Sub SetColumnHorizontalAlignment(Col As B4XTableColumn, Alignment As String)
    For i = 1 To Col.CellsLayouts.Size - 1
        Dim p As B4XView = Col.CellsLayouts.Get(i)
        p.GetView(Col.LabelIndex).SetTextAlignment("CENTER", Alignment)
    Next
End Sub
 
Upvote 0
Top