B4J Question TextArea_SelectionChanged Event?

Discussion in 'B4J Questions' started by xulihang, Sep 26, 2018.

  1. xulihang

    xulihang Active Member Licensed 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:

    Code:
    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.

    Code:
    #RaisesSynchronousEvents: SubThatCanRaiseEvent
     
    Last edited: Sep 26, 2018
  2. stevel05

    stevel05 Expert Licensed 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.

    Code:
    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
     
    Erel, xulihang and Daestrum like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice