B4J Question Get Font For Cell In Tableview?

keirS

Well-Known Member
Licensed User
Longtime User
Can't wok out how to do this using JavaObject. I know I need to query the cellfactory for the cell or the column but this doesn't work

B4X:
Dim TableViewJO As JavaObject = TableView1
For ColumnCntr = 0 To TableView1.ColumnsCount - 1
 Dim ColumnFont As Font
 ColumnFont=TableViewJO.RunMethodJO("getColumns",Null).RunMethodJO("get",Array(ColumnCntr)).RunMethodJO("getCellFactory",Null).RunMethodJO("getFont",Null)
Next
 

keirS

Well-Known Member
Licensed User
Longtime User
I am trying to build a generic way to calculate the column sizes for a Tableview. The Tableview is dynamic so can have many columns.

At the moment I am just getting the font size for the whole TableView and assuming it's using the default font.

B4X:
TVFont = fx.DefaultFont(CSSUtils.GetStyleProperty(ExcelTableView,"-fx-font-size"))

Then I use a TextBuilder to get the width of the text.

B4X:
Sub Class_Globals
    Private JOTextMetric As JavaObject
  
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    JOTextMetric.InitializeStatic("javafx.scene.text.TextBuilder")
  
  
End Sub
Public Sub getTextWidth(Text As String,TextFont As Font) As Int
    Return JOTextMetric.RunMethodJO("create",Null).RunMethodJO("text",Array(Text)).RunMethodJO("font",Array(TextFont)).RunMethodJO("build",Null).RunMethodJO("getLayoutBounds",Null).RunMethod("getWidth",Null)
  
 
End Sub
Public Sub getTextHeight(Text As String,TextFont As Font) As Int
    Return JOTextMetric.RunMethodJO("create",Null).RunMethodJO("text",Array(Text)).RunMethodJO("font",Array(TextFont)).RunMethodJO("build",Null).RunMethodJO("getLayoutBounds",Null).RunMethod("getHeight",Null)
  
 
End Sub
 
Upvote 0
Top