B4J Question Auto Complete TextField problem

zed

Active Member
Licensed User
Hi all,

I have a problem with a java command.
With this code, the list is cached.
Java:
Sub SetAutoComplete(Items As List, Field As TextField)
    Dim jo As JavaObject
    jo.InitializeStatic("org.controlsfx.control.textfield.TextFields")
    jo.RunMethod("bindAutoCompletion", Array(Field, Items))
End Sub
How to delete cache list.
When the data is modified, it is always the old list which is used and not the modified list

Thanks
 
Solution
Or a better version that doesn't require extra fields in globals ( it packs the autocomplete and binding into the textfield tag) but is harder to follow - sorry.

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Private words As List
    Private TextField1 As TextField
    Private Button2 As B4XView
    Type autobind (autocomplete As JavaObject, binding As JavaObject)
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    words.Initialize
    words.Add("one")
    words.Add("two")
    words.Add("three")
    words.Add("four")
    createBinding(TextField1)...

Daestrum

Expert
Licensed User
Longtime User
Are you calling SetAutoComplete(...) after changing the List of suggestions. Seems to work ok here, if it is called after you change the list contents, it then displays the correct data in the list.

But, I did change the code a tad, I made the jo live in globals and only initialized it once. (As it seemed to show 2 drop downs if it was created again)

B4X:
Sub SetAutoComplete(Items As List, Field As TextField)
    If autocomplete.IsInitialized = False Then
        autocomplete.InitializeStatic("org.controlsfx.control.textfield.TextFields")
    End If
    autocomplete.RunMethod("bindAutoCompletion", Array(Field, Items))
End Sub
 
Last edited:
Upvote 0

zed

Active Member
Licensed User
Here's how I do
B4J:
Sub LoadData
---
---
Do While RS.NextRow
        Dim codeId As Int
        Dim sProd, sTit, sDat As String
        codeId = RS.GetInt("sCodeId")
        sProd = RS.GetString("sProduct")
        sTit = RS.GetString("sTitle")
        sDat = RS.GetString("sDate")
        xclv.Add(CreateItem(xclv.AsView.Width,codeId,sProd,sTit,sDat),codeId)
        Data.Add(sTit)
        nbrItem = nbrItem+1
    Loop
    RS.Close
    lblNbrItem.Text =  $"# ${nbrItem}"$
    Log(Data.Size)
    SetAutoComplete(Data,tfSearch)
    
End Sub

Sub SetAutoComplete(Items As List, Field As TextField)
    Dim jo As JavaObject
    jo.InitializeStatic("org.controlsfx.control.textfield.TextFields")
    jo.RunMethod("bindAutoCompletion", Array(Field, Items))
End Sub
 
Upvote 0

zed

Active Member
Licensed User
It does not work. I have 2 lists and it doesn't match.
 

Attachments

  • view.png
    view.png
    11.9 KB · Views: 130
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
ok on further testing you have to dispose of the autocompletebinding or it just adds new drop downs.

example (hopefully clear enough for you to incorporate into your code)
The form has 1 textfield and 2 buttons

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI 
    Private Button1 As B4XView
    Private words As List
    Private TextField1 As TextField
    Private TextField1autocomplete As JavaObject
    Private textField1binding As JavaObject
    Private Button2 As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    words.Initialize
    words.Add("one")
    words.Add("two")
    words.Add("three")
    words.Add("four")
    TextField1autocomplete.InitializeStatic("org.controlsfx.control.textfield.TextFields")
    TextField1.Tag = TextField1autocomplete
    ReBind(words,TextField1)
End Sub

Sub Button1_Click ' add some words to list
    words.Add("five")
    words.Add("six")
    ReBind(words,TextField1)  ' call the bind again
End Sub

Sub Button2_Click ' add more words to list
    words.Add("favourite")
    words.Add("florence")
    ReBind(words,TextField1)  ' call the bind again
End Sub

Sub ReBind(items As List, Field As TextField)
    If textField1binding.IsInitialized = True Then
        textField1binding.RunMethod("dispose",Null) ' kill of existing autocomplete
    End If
    textField1binding = Field.Tag.As(JavaObject).RunMethod("bindAutoCompletion", Array(Field, items))
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Or a better version that doesn't require extra fields in globals ( it packs the autocomplete and binding into the textfield tag) but is harder to follow - sorry.

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Private words As List
    Private TextField1 As TextField
    Private Button2 As B4XView
    Type autobind (autocomplete As JavaObject, binding As JavaObject)
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    words.Initialize
    words.Add("one")
    words.Add("two")
    words.Add("three")
    words.Add("four")
    createBinding(TextField1)
    ReBind(words,TextField1)
End Sub

Sub createBinding(tf As TextField)
    Dim tmpbind As autobind
    tmpbind.Initialize
    tmpbind.autocomplete.InitializeStatic("org.controlsfx.control.textfield.TextFields")
    tf.Tag = tmpbind
End Sub

Sub Button1_Click ' add some words to list
    words.Add("five")
    words.Add("six")
    ReBind(words,TextField1)  ' call the bind again
End Sub

Sub Button2_Click ' add more words to list
    words.Add("favourite")
    words.Add("florence")
    ReBind(words,TextField1)  ' call the bind again
End Sub

Sub ReBind(items As List, Field As TextField)
    If Field.Tag.As(autobind).binding.IsInitialized = True Then
        Field.Tag.As(autobind).binding.RunMethod("dispose",Null) ' kill off existing autocomplete
    End If
' save the new binding in the tag
    Field.Tag.As(autobind).binding = Field.Tag.As(autobind).autocomplete.RunMethod("bindAutoCompletion", Array(Field, items))
End Sub
 
Last edited:
Upvote 0
Solution
Top