B4J Tutorial Mix text and icon on button and other nodes

Hello!

Just recently a customer told me that my app was... boring... that it lacked some images, so after crying out loud i remember that we can put icons but i still needed the text.

so... Ta Da!

B4X:
Utils.iconAndText(rolBtn0,0xF2BA,"Appoinment")

public Sub iconAndText(btn As Button,fontAwesome As String,text As String)
 
    btn.Font = fx.CreateFontAwesome(35)
    btn.Text = Chr(fontAwesome)
 
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Font = fx.CreateFont(fx.DefaultFont(16).FamilyName,16,True,False)
    lbl.Text = text
    lbl.TextSize = 16

    Dim jBtn As JavaObject = btn
    jBtn.RunMethod("setGraphic",Array(lbl))
End Sub

This code will work for any node that has the Labeled inheritance and Text, for example:
Label, Button, checkbox, etc.
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Labeled.html

You can actually change where is the icon shown (righ, left, top, bottom) but i am not smart enough to do it without java

B4X:
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;

static public void setContentDisplayBottom (Button btn) {
    btn.setContentDisplay(ContentDisplay.BOTTOM);
}

static public void setContentDisplayRight (Button btn) {
    btn.setContentDisplay(ContentDisplay.RIGHT);
}
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
After reading this amazing tutorial:
https://www.b4x.com/android/forum/threads/access-enums.51286/

you can modify the direction of the graphic like this:
B4X:
    Dim jBtn As JavaObject = btn
    jBtn.RunMethod("setGraphic",Array(lbl))
   
    Dim nContent As JavaObject
    nContent.InitializeStatic("javafx.scene.control.ContentDisplay")
   'TOP, BOTTOM, etc. 
   ' https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Labeled.html
    Dim enumClass As Object = nContent.RunMethod("valueOf",Array("TOP"))
   
    jBtn.RunMethod("setContentDisplay",Array(enumClass))
 
Top