B4J Question Available fonts showing fontstyle

ThRuST

Well-Known Member
Licensed User
Longtime User
Hello, I have this code which is my attempt to show all available fonts and have each displayed in the combobox in a label. I attach the source so you can try it. What changes is needed to make it work?
Please post the solution as an update source code. Prefferably, here's my source.
 

Attachments

  • AvailableFonts.zip
    17.7 KB · Views: 200

ThRuST

Well-Known Member
Licensed User
Longtime User
This is what the source code looks like

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Comboboxfont As ComboBox
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show

    ListAvailableFonts   
    
End Sub
    
Sub ListAvailableFonts
    
    Dim fNo As Int
    Dim lbl As Label
    lbl.Initialize("lbl")
    
    Comboboxfont.Items.Clear
    For Each familyname As String In fx.GetAllFontFamilies
        
        lbl.Text = familyname & fNo
        lbl.Font = fx.CreateFont(familyname, 22, False,False)
        fNo = fNo + 1
        lbl.Tag = fNo
        
        Comboboxfont.Items.Add(lbl.Text)
        
    Next
    
End Sub


'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Change
B4X:
Sub ListAvailableFonts
    
    Dim fNo As Int
    Dim lbl As Label
    lbl.Initialize("lbl")
    
    Comboboxfont.Items.Clear
    For Each familyname As String In fx.GetAllFontFamilies
        
        lbl.Text = familyname & fNo
        lbl.Font = fx.CreateFont(familyname, 22, False,False)
        fNo = fNo + 1
        lbl.Tag = fNo
        
        Comboboxfont.Items.Add(lbl.Text)
        
    Next
    
End Sub

to
B4X:
Sub ListAvailableFonts
    
 Dim fNo As Int
 Comboboxfont.Items.Clear
 For Each familyname As String In fx.GetAllFontFamilies
  Dim lbl As Label  ' <<<<<    initialize the label inside the loop
  lbl.Initialize("lbl")
  lbl.Text = familyname & fNo
  lbl.Font = fx.CreateFont(familyname, 22, False,False)
  fNo = fNo + 1
  lbl.Tag = fNo
        
  Comboboxfont.Items.Add(lbl)   ' <<< add the label not just the text
        
 Next
    
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@Daestrum Thanks, nice to know I was so close to make it work. Sorry for not commenting anything in the code.
As you can see in the screenshot, and you probably noticed it too is that some fonts creates a huge gap.
Any idea how to solve that? I'd like to have the window taller so more fonts can be seen, that would look nicer.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Ops sorry forgot to add the screenshot

Capture.JPG
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Notice how the combobox turned into one large box. I'l like to keep it as a small clickable bar. This must indeed be corrected :)
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
How would you read which item that were clicked since the label is displayed as an object and not at string? To get the selected font name?

B4X:
Sub Comboboxfont_ValueChanged (Value As Object)

     fx.Msgbox(MainForm, Value , "")
   
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Made a change to the logic to keep the combo the same height (needs javaobject library)
B4X:
Sub ListAvailableFonts
 
    Dim fNo As Int

    Comboboxfont.Initialize("cmb")  ' I need this line as I don't use the layout
    Comboboxfont.Items.Clear

    For Each familyname As String In fx.GetAllFontFamilies
        Dim lbl As Label
        lbl.Initialize("lbl")
        lbl.Text = familyname & "  ["&fNo&"]"
        fNo = fNo + 1
        lbl.Tag = fNo
        lbl.TooltipText = lbl.Text
        Dim tt As JavaObject = asJO(lbl).RunMethodJO("getTooltip",Null)
        tt.RunMethod("setFont",Array(fx.CreateFont(familyname, 22, False,False)))
' the two lines above can be replaced with the one line below, but harder to understand
' asJO(lbl).RunMethodJO("getTooltip",Null).RunMethod("setFont",Array(fx.CreateFont(familyname, 22, False,False)))
        ' tootip shows the font
        Comboboxfont.Items.Add(lbl)
    
    Next
 
End Sub
and use (or _ValueChanged) you just need the Value
B4X:
Sub cmb_SelectedIndexChanged(Index As Int, Value As Object)
    Dim f As Font = asJO(Value).RunMethodJO("getTooltip",Null).RunMethod("getFont",Null)
        ' f now contains the font selected from the combobox
End Sub

uses this
B4X:
Sub asJO(o As JavaObject) As JavaObject
    Return o
End Sub
 
Last edited:
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@Daestrum Do you mind posting your source code, all changes is causing problems. Now I cannot see the font styles only as text, so source code will be great. Thanks
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
If you can adjust your solution to use the layout "1" I made it would be great since the internal designer is used for layouts anyway.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Ok this is the entire code ( needs javaobject library)
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
	Private fx As JFX
	Private MainForm As Form
	Private Comboboxfont As ComboBox

End Sub

Sub AppStart (Form1 As Form, Args() As String)
	MainForm = Form1
	MainForm.RootPane.LoadLayout("1") 'Load the layout file.
	MainForm.Show

	ListAvailableFonts


End Sub
    
Sub ListAvailableFonts
    
	Dim fNo As Int


	Comboboxfont.Items.Clear
	
	For Each familyname As String In fx.GetAllFontFamilies
        Dim lbl As Label
		lbl.Initialize("lbl")
		lbl.Text = familyname & "  ["&fNo&"]"
		fNo = fNo + 1
		lbl.Tag = fNo
		lbl.TooltipText = lbl.Text
		asJO(lbl).RunMethodJO("getTooltip",Null).RunMethod("setFont",Array(fx.CreateFont(familyname, 22, False,False)))

		Comboboxfont.Items.Add(lbl)
        
	Next
    
End Sub
Sub cmb_ValueChanged (Value As Object)
	Dim f As Font = asJO(Value).RunMethodJO("getTooltip",Null).RunMethod("getFont",Null)
	' f is the selected font
End Sub
Sub asJO(o As JavaObject) As JavaObject
	Return o
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
	Return True
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@Daestrum This is what it looks like. I want the font styles to be shown in the labels directly. Does it look the same for you? I use Java v9.0.4.

Capture.JPG
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You cannot have the combobox height constant if you use the font in the items.

(not 100% true - you could edit the skinbase of the combobox to change the font of the label it shows in the selected item at top. This is far more work than it's worth)
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
This does not trigger

B4X:
Sub cmb_ValueChanged (Value As Object)
    Dim f As Font = asJO(Value).RunMethodJO("getTooltip",Null).RunMethod("getFont",Null)
    ' f is the selected font
    fx.Msgbox (MainForm, asJO(Value), "")
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
ok forget about the font height problem. It's better to have the font styles shown in the labels anyway. The height problem is not important (my oppinion)
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I might have to leave the option out to display each fontstyle in the the combobox because of the complexity in displaying those fonts. I display them by name it looks better, but was not really what I wanted. But I've learned to choose my battles. @Daestrum Thanks alot for your efforts to sort these issues out.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
fx.Msgbox (MainForm, asJO(Value), "")

Did you want the text from the label ?
B4X:
fx.Msgbox (MainForm, asJO(Value).RunMethod("getText",null), "")
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@Daestrum Your solution is actually pretty nice, as an alternate solution with the fontstyle in the tooltip :)

However the name is not shown when I select an item. Am I missing something?

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    
    Private fx As JFX
    Private MainForm As Form
    Private Comboboxfont As ComboBox

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show

    ListAvailableFonts

End Sub
    
Sub ListAvailableFonts
    
    Dim fNo As Int

    Comboboxfont.Items.Clear
    
    For Each familyname As String In fx.GetAllFontFamilies
        Dim lbl As Label
        lbl.Initialize("lbl")
        lbl.Text = familyname & "  ["&fNo&"]"
        fNo = fNo + 1
        lbl.Tag = fNo
        lbl.TooltipText = lbl.Text
        asJO(lbl).RunMethodJO("getTooltip",Null).RunMethod("setFont",Array(fx.CreateFont(familyname, 22, False,False)))

        Comboboxfont.Items.Add(lbl)
        
    Next
    
End Sub

Sub cmb_ValueChanged (Value As Object)
    fx.Msgbox (MainForm, asJO(Value).RunMethod("getText",Null), "")
End Sub

Sub asJO(o As JavaObject) As JavaObject
    Return o
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

This is how I show the selected font in my project

B4X:
Sub comboboxfont_ValueChanged (Value As Object)

    ' Change CodeArea font
    CSSUtils.SetStyleProperty(CodeArea1.CustomViewNode,"-fx-font-family", Value)
    
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I also think that the tooltip wait should be instant. There's no meaning to wait 3-5 seconds for the fontstyle to show up. It's reasonable to be picky :D
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
my mistake, I forgot to change a line after I used your layout
B4X:
Sub Comboboxfont_ValueChanged (Value As Object)
I had
B4X:
Sub cmb_ValueChanged (Value As Object)
 
Upvote 0
Top