B4J Question BUTTON - Change color if mouse cursor is over


Well-Known Member
Licensed User
Longtime User
Dear friends,

please. I can change color of button:
button.style="-fx-background-color: Orange;"

it change the default color, it is great, but function of button is LOST = change color of button if mouse cursor is over

please, how can I put some other color for this situation = if mouse cursor is over this button?
Thank you very much


Licensed User
Longtime User
Here's some code I wrote a while back to change the cursor when over a button, should be easy to modify to change the colour instead.
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim cursor As JavaObject
    Dim b As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    b.Text = "Hello"
    cursor.InitializeStatic("javafx.scene.Cursor")  ' cursors, so we can use their names ie HAND, WAIT etc
    setHandler(b,"setOnMouseEntered","b_enter") ' to change cursor on entry
    setHandler(b,"setOnMouseExited","b_exit")    ' change back on exit
End Sub
Sub setHandler(ob As Object,eventName As String,handlerName As String)
    asJavaObject(ob).RunMethod(eventName, Array(asJavaObject(ob).CreateEventFromUI("javafx.event.EventHandler",handlerName,False)))
End Sub
Sub b_enter_Event(MethodName As String, Args() As Object)
    asJavaObject(b).RunMethod("setCursor",Array(cursor.GetField("HAND"))) ' set cursor to hand
End Sub
Sub b_exit_Event(MethodName As String, Args() As Object)
    asJavaObject(b).RunMethod("setCursor",Array(cursor.GetField("DEFAULT"))) ' set back to default
End Sub
Sub asJavaObject(j As JavaObject) As JavaObject
    Return j
End Sub
Upvote 0