B4J Question How to get the cursor position (line column) of textarea

Pietro Pancino

Member
Licensed User
Longtime User
Hi all,
I need to get the content of the line where is the cursor in a textarea, is anyone know how to do it?
:) Pietro
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Pietro Pancino

Member
Licensed User
Longtime User
Thank you for your answer :)
I tried it, but it give you the absolut position of the cursor from the begining of the text but not in line column format...
I look everywhere but i'm blocked. In fact I need to send the content of the line where is the cursor to a specific interface through TCPIP for test purpose...
 
Upvote 0

Pietro Pancino

Member
Licensed User
Longtime User
Hi again!
In fact... I did it starting from the code you mentioned (https://www.b4x.com/android/forum/t...-a-selectionchanged-event.103119/#post-646376)
It's working, here is the code and the zip file.
Many Thanks to you!
:):):)
Pietro


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

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
       
    Private textarea1 As TextArea
    Private nb_line As Label
    Private coordonate As Label
    
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    
    Dim jo As JavaObject = textarea1
    textarea1.Text="line 1"&CRLF&"line 2"&CRLF&"line 2"&CRLF&"line 4"&CRLF&CRLF&"end line"
    Dim event As Object = jo.CreateEventFromUI("javafx.beans.value.ChangeListener", "SelectionChanged", Null)
    jo.RunMethodJO("selectionProperty", Null).RunMethod("addListener", Array(event))

    
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub




Sub SelectionChanged_Event (MethodName As String, Args() As Object) As Object
    If MethodName = "changed" Then
        Dim IndexRange As JavaObject = Args(2)
        Dim StartPosition As Int = IndexRange.RunMethod("getStart", Null)
        'Dim EndPosition As Int = IndexRange.RunMethod("getEnd", Null)
        
        ' copy the text area to a list
        Dim l As List
        l.Initialize
        l.AddAll(Regex.Split(CRLF, textarea1.text))
        
        ' echo the number of line excluding empty line at the end
        nb_line.Text=l.size& " lines in text area"
        
        Dim the_start,the_end,line,column As Int       
        Dim the_str As String =""
        
        the_start = 0
        the_end = 0
        
        For i = 0 To l.Size-1
            ' get line length
            the_str = l.Get(i)
            the_end = the_start+the_str.Length
            
            If StartPosition >= the_start And StartPosition <= the_end Then
                line = i+1
                column= StartPosition - the_start
            End If           
            the_start = the_end+1
            coordonate.Text ="line = "&line& " column = "&column
        Next
        
    End If
    Return Null
    
    
End Sub
 

Attachments

  • line_column_in_textarea.zip
    2.6 KB · Views: 179
Upvote 0
Top