B4J Question TextArea_SelectionChanged Event?

xulihang

Active Member
Licensed User
Longtime User
Hi,

The textarea control has not selectionchanged event. I have tried to implement this event using jreflection. I set the property to selectionProperty and can get the indexrange in the changed event.

I am not familiar with reflection and javafx. Am I doing it right?

The code I use:

B4X:
Sub addSelectionChangedEvent(textarea1 As TextArea,eventName As String)
    Dim Obj As Reflector
    Obj.Target = textarea1
    Obj.AddChangeListener(eventName, "selectionProperty")
   
End Sub


Sub sourceTextAreaSelection_changed(old As Object, new As Object)
    Log(GetType(new))
    Dim ta As TextArea
    ta=sender
    Dim indexString As String
    indexString=new
    Dim selectionStart,selectionEnd As Int
    selectionStart=Regex.Split(",",indexString)(0)
    selectionEnd=Regex.Split(",",indexString)(1)
    If selectionEnd<>selectionStart Then
        Main.sourceTermTextField.Text=ta.Text.SubString2(selectionStart,selectionEnd)
    End If
End Sub

I also come across the RaisesSynchronousEvents error. The app does not crash. I add the code below to avoid the error log.

B4X:
#RaisesSynchronousEvents: SubThatCanRaiseEvent
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
I would be tempted to use JavaObject, it is a little more work to set up the listener but by using CreateEventFromUI it avoids the RaisedSynchronousEvent error.

Also, once you have the Textarea in the callback, you can access the selected text from the Text area directly. This will work with either option.

B4X:
Sub addSelectionChangedEvent(textarea1 As TextArea,eventName As String)
    Dim JO As JavaObject = textarea1
       Dim Prop As JavaObject = JO.RunMethod("selectionProperty",Null)
    Dim O As Object = JO.CreateEventFromUI("javafx.beans.value.ChangeListener","SelectionChanged",Null)
    Prop.RunMethod("addListener", Array(O))
End Sub


Private Sub SelectionChanged_Event (MethodName As String, Args() As Object)
    Dim Ta As TextArea = Sender
    Main.sourceTermTextField.Text=Ta.Text.SubString2(Ta.SelectionStart,Ta.SelectionEnd)
End Sub
 
Upvote 0
Top