B4J Question TextFlow: Adding Images & Other Fonts

Mashiane

Expert
Licensed User
Longtime User
Hi there

I'm trying to have an AddImage method in the TextFlow class.

B4X:
Public Sub AddImage(dir As String, img As String)
    Dim uri As String = File.GetUri(dir, img)
    Dim lastItem As JavaObject
    lastItem.InitializeNewInstance("javafx.scene.image.Image", Array(uri))
    texts.Add(lastItem)
End Sub

Well, this is not working. What I want to do is to change the text color of a treeview item and then assign an existing image to it as the current color change does not pick up the treeitem image.

B4X:
'Description: Set TreeItem text to some color you want.
'Tag: TreeItem, color, font size
Sub TreeViewItemSetColorImage(ti As TreeItem, Color As Paint, FontSize As Int, dir As String, img As String)
    Dim ft As Font = fx.DefaultFont(FontSize)
    Dim text As String = ti.Text
    Dim tf As TextFlow
    tf.Initialize
    tf.AddImage(dir,img)
    tf.AddText(text).SetColor(Color).SetFont(ft)
    Dim jo As JavaObject = ti
    Dim p As Pane = tf.CreateTextFlow
    jo.RunMethod("setGraphic", Array(p))
End Sub

Can anyone advise please??? Thanks

TextFlow.png
 

Mashiane

Expert
Licensed User
Longtime User
Thanks a lot Erel, that did the magic, wow!... I resoluted to this method instead. Now, as the text property is very important I still need it but now due to the setGraphic method, I seem to have two labels.

Just one last thing: Is there some magic to make the TreeView text invisible/transparent so that I dont have two texts showing?

B4X:
Public Sub AddImage(dir As String, img As String) As TextFlow
    Dim simg As Image = fx.LoadImage(dir,img)
    Dim uri As ImageView
    uri.Initialize("")
    uri.SetImage(simg)
    uri.SetLayoutAnimated(0,0,0,16,16)
    texts.Add(uri)
    Return Me
End Sub

public Sub AddImage1(img As Image, imgw As Double, imgh As Double) As TextFlow
    Dim uri As ImageView
    uri.Initialize("")
    uri.SetImage(img)
    uri.SetLayoutAnimated(0,0,0,imgw,imgh)
    texts.Add(uri)
    Return Me
End Sub

'Description: Set TreeItem text to some color you want.
'Tag: TreeItem, color, font size
Sub TreeViewItemSetColorImage(ti As TreeItem, Color As Paint, FontSize As Int, dir As String, img As String)
    Dim ft As Font = fx.DefaultFont(FontSize)
    Dim text As String = ti.Text
    Dim tf As TextFlow
    tf.Initialize
    tf.AddImage(dir,img).AddText(" ")
    tf.AddText(text).SetColor(Color).SetFont(ft)
    Dim jo As JavaObject = ti
    Dim p As Pane = tf.CreateTextFlow
    p.Tag = ti.text
    jo.RunMethod("setGraphic", Array(p))
End Sub

AlmostThere.png

The red portion is done by SetGraphic and the black labels are the original treeitem text.
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Set ti.Text = "" after you get the text.
If I do that, my treeview SelectedItemChanged and TreeViewItemSetColorImage above will not work on subsequent calls as they use the .Text property of the tree item.

However if I set the pane tag property on creation of the treeitem using TextFlow and then be able to read it back, it will work.

Assuming that I create a treeitem like this..

B4X:
Dim jo As JavaObject = ti
    Dim p As Pane = tf.CreateTextFlow
    p.Tag = "Tree Item"
    jo.RunMethod("setGraphic", Array(p))


How can I get that tag property back?
 
Upvote 0
Top