B4J Question Font not initialized....why?

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
This is the Sub
B4X:
Sub LoadFonts(n As Node)
    Dim cbox As ComboBox = n
    Dim l As List
    l.Initialize
    l = File.ListFiles(File.DirApp & "\Fonts\")
    If l.Size = 0 Then Return
    For i = 0 To l.Size -1
        Dim lb As Label
        lb.Initialize("")
        lb.Font = fx.LoadFont(File.DirApp & "\Fonts", l.Get(i), 14) '<--- Error
        Dim s As String = l.Get(i)
        lb.Text = s.SubString2(0, s.LastIndexOf("."))
        cbox.Items.Add(lb)
    Next
End Sub

java.lang.RuntimeException: Object should first be initialized (Font).
Thanks
 

Cableguy

Expert
Licensed User
Longtime User
You should use it like this:

B4X:
Dim myfont as typeface
myfont.initialize(....)

Lb.font=myfont
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Are you trying to load a non ttf font?

Try this

B4X:
Sub listFonts(n As Node)
 Dim cbox As ComboBox=n
 Dim f As JavaObject
 f.InitializeStatic("javafx.scene.text.Font")
 Dim l As List = f.RunMethod("getFontNames",Null) ' will list all fonts registered on system
 For Each fn As String In l
  Dim lb As Label
  lb.Initialize("")
  Dim f As JavaObject
  If fn.ToLowerCase.Contains("bold") And fn.ToLowerCase.Contains("italic") Then
   f = fx.createfont(fn,14.0D,True,True)
  else if fn.ToLowerCase.Contains("bold") Then
   f = fx.createfont(fn,14.0D,True,False)
  else if fn.ToLowerCase.Contains("italic")Then
   f = fx.createfont(fn,14.0D,False,True)
  Else
   f = fx.createfont(fn,14.0D,False,False)
  End If
  lb.Font = f
  lb.Text = fn.trim
  cbox.Items.Add(lb)
 Next
End Sub
 
Last edited:
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Thank you all for the support
the problem of initialization is because I don't use it
DirAssets but with:
B4X:
Try
      lb.Font = fx.LoadFont(File.DirApp & "\Fonts", l.Get(i), 16)
Catch As Exception
End Try
also if raised the exception it works well.
Now I try to use your code Daestrum.
Thank you again.
 
Upvote 0
Top