B4J Question Textfield and highlighted text problem

drgottjr

Expert
Licensed User
Longtime User
the app calls for the user to key in a series of values in a textfield view. she hits the RETURN key or clicks on a button, and the app does its thing. as a courtesy, the app saves the values that were keyed in for next time (the user will tend to reuse them). no need to key them in again. sometimes the user might add some more values. no need to start from the beginning; she only need type in the additions. the entire set of values will be saved automatically. (of course, the user can clear the set and start anew at any time.) on launch the app populates the textfield view with the saved values, if any. if there are none, it just waits for user input.

the app was ported from B4A where it behaved as expected. there is a problem, however when porting the app to B4J to run on a windows pc. please refer to the attached image.

if i set a textfield's text (as described above: assigning it either manually or by reading from a file), the text is highlighted as is visible in the image. not only that, but the cursor is positioned at the start of the first character. if the user starts to type, the entire text is erased. i don't want any of that behavior. by the way, if i set the text in the designer, the same thing happens. so it's consistent from all angles.

how do i avoid the text's being highlighted?

1) i thought about trying to "unrequest" the focus for that view. i don't see how to accomplish that.

2) i tried using a second, tiny dummy textfield view and having it request focus away from the main textfield view. that works, but only if the second textfield is visible (which i don't want; no matter how small you make the dummy view, the user still sees a cursor blinking inexplicably in some random
spot of the screen.)

3) i have noticed that if i simply click on the mouse (while within the textfield view), the highlighting disappears, the text remains, and the mouse magically moves from the first character to 1 character after the current end of the text. that is, of course, exactly what i want to see, but without having to click the mouse. plus it's silly to have to tell the user "please click on mouse to remove highlighting".

so i thought i would try to simulate a mouse click. in B4A i just call a view's "click" sub as if i had clicked on it, but it doesn't seem to be the same thing in B4J. i also tried the jAWTrobot library by calling its mousepress, mouserelease methods one after another, as if clicking. i assumed the standard B4J "MouseClicked" sub for the view would see that, but it doesn't...

4) i tried to massage the textfield's selectionend and selectionstart properties, but couldn't get the right combination of values to have any effect.

thanks in advance for any suggestions.
-go
textfield.jpg
 

stevel05

Expert
Licensed User
Longtime User
You'll need to do something like this:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TextField1 As TextField
   
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
   
    TextField1.Text = "Test"
   
    CallSubDelayed3(Me,"ClearSelection",TextField1,True)
   
End Sub

Sub ClearSelection(Field As TextField,CursorAtEnd As Boolean)
    Dim CursorPos As Int = 0
    If CursorAtEnd Then CursorPos = Field.Text.Length
    Field.SetSelection(CursorPos,CursorPos)
End Sub

You need to use CallSubDelayed If you are calling the sub from AppStart (Or any sub called directly from there).
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
You'll need to do something like this:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TextField1 As TextField
  
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
  
    TextField1.Text = "Test"
  
    CallSubDelayed3(Me,"ClearSelection",TextField1,True)
  
End Sub

Sub ClearSelection(Field As TextField,CursorAtEnd As Boolean)
    Dim CursorPos As Int = 0
    If CursorAtEnd Then CursorPos = Field.Text.Length
    Field.SetSelection(CursorPos,CursorPos)
End Sub

You need to use CallSubDelayed If you are calling the sub from AppStart (Or any sub called directly from there).
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
brilliant! i had tried your Field.SetSelection(CursorPos,CursorPos) on my own but without the callsubdelay it had no effect, apparently. why? can you point me to some documentation, pls? thks
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I can't find any specific documentation, but until the Appstart sub has completed, the Gui is still being built. Using Callsubdelayed adds the call to the sub to a queue, and will be processed after the currently running code has completed.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
I can't find any specific documentation, but until the Appstart sub has completed, the Gui is still being built. Using Callsubdelayed adds the call to the sub to a queue, and will be processed after the currently running code has completed.

it is what it is. i also tried if mainform.showing to see if that maybe acted like javascript's "onload". no deal. thanks again.
 
Upvote 0
Top