B4J Code Snippet Property Listeners Class

SubName: Class SimpleObjectProperty

Description:
If you are writing a process that waits for a specific event to do something, such as a game score maybe, you don't need to keep checking a variable to see if it occurs. We can get the java to tell us using properties and listeners. This code uses a simpleobjectproperty and a listener. There are properties for Integer, String, Boolean and other types it you want to be more specific. See the documentation here: https://docs.oracle.com/javase/8/ja...vafx/beans/property/SimpleObjectProperty.html

The downside is that we have to set the property values thus : S.Value = 20 rather than just S=20.

This is a simple example, but you can do with it what you like.

This is the code for a SimpleObjectProperty class

Code:
B4X:
Sub Class_Globals
    Private fx As JFX
    Private JO As JavaObject
    Private mModule As Object
    Private mEventName As String
    Private Listener As Object
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(Module As Object,EventName As String)
    JO.InitializeNewInstance("javafx.beans.property.SimpleObjectProperty",Array(Me,"value"))
    mModule = Module
    mEventName = EventName
    Listener = JO.CreateEvent("javafx.beans.InvalidationListener","TextChanged","")
    JO.RunMethod("addListener",Array(Listener))
End Sub
Public Sub setValue(Value As Object)
    JO.RunMethod("set",Array(Value))
End Sub
Public Sub getValue As Object
    Return JO.RunMethod("get",Null)
End Sub
Sub TextChanged_Event(MethodName As String,Args() As Object) As Object        'ignore
    If SubExists(mModule,mEventName&"_Event") Then CallSub2(mModule,mEventName & "_Event",getValue)
    Return False
End Sub
Public Sub RemoveListener
    JO.RunMethod("removeListener",Array(Listener))
End Sub

And an example of it's usage ( as Main Module):

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

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Interrupt As Boolean
End Sub

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

    Dim S As SimpleObjectProperty
    S.Initialize(Me,"ObjectChanged")
    Do While Not(Interrupt)
        S.Value = Rnd(0,100)
        Log(S.Value)
    Loop
End Sub

Sub ObjectChanged_Event(NewValue As Object)
    If NewValue > 90 Then
        Log("Achieved " & NewValue)
        Interrupt = True
    End If
End Sub

It's not a great example as it will block the Gui and anything else from happening, but you get the idea.

As you can see from the test, the listener fires before the next line of code is run. So it is an instant notification.

Depends on: JavaObject

Tags: B4j Observable Property
 
Last edited:

inakigarm

Well-Known Member
Licensed User
Longtime User
SubName: Class SimpleObjectProperty

It's not a great example as it will block the Gui and anything else from happening, but you get the idea.

And with Sleep, simple way to UI refreshing ---> set a progressindicator.Progress=-1 and see
B4X:
Dim count as Int=0
Do While Not(Interrupt)
        S.Value = Rnd(0,10000)
        If count Mod 10=0 Then Sleep(0)
        Log(S.Value)
        count=count+1
        Log("Count= "& count)
Loop
 
Top