7

I'm currently working on a JavaFX project.I'm using Autcomplete TextField of ControlFx .Each time i add new rows in database table, it should to update Autocomplete ,i did this but my problem is showing double Context-Menu ,we can say double autocompletes because i call method that create autocomplete each adding of new elements in table.

When i click a tab editBill i call this method :

public void showEditBill() {
    if (!BillPane.getTabs().contains(EditBillTab)) {
        BillPane.getTabs().add(EditBillTab);
    }
    SingleSelectionModel<Tab> selectionModel = BillPane.getSelectionModel();
    selectionModel.select(EditBillTab);
    /*it should remove the old autocomplete from textfield*/
    pushBills(); //Call for cheking new items
}

pushBills method () :

public void pushBills() {    
    ArrayList list = new ArrayList<>();
    bills = new BillHeaderDao().FindAll();
    for (int i = 0; i < bills.size(); i++) {
        list.add(bills.get(i).getIdClient());
    }
    //How can i remove the old bind before bind again
    autoCompletionBinding = TextFields.bindAutoCompletion(SearchBill, SuggestionProvider.create(list));
}

How i can remove the old autocomplete and bind new automplete?

Description of problem

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36

4 Answers4

7

Just in any case if you need to keep instance of AutoCompletionTextFieldBinding object, thus avoiding use of:

autoCompleteBinding = TextFields.bindingAutoCompletion(TextField,List);

, which will change the instance, we could go a little bit deeper and use this:

// let's suppose initially we have this possible values:
Set<String> autoCompletions = new HashSet<>(Arrays.asList("A", "B", "C"));
SuggestionProvider<String> provider = SuggestionProvider.create(autoCompletions);
new AutoCompletionTextFieldBinding<>(textField, provider);

// and after some times, possible autoCompletions values has changed and now we have:
Set<String> filteredAutoCompletions = new HashSet<>(Arrays.asList("A", "B"));

provider.clearSuggestions();
provider.addPossibleSuggestions(filteredAutoCompletions);

So, through SuggestionProvider, we have "updated" auto completion values. To avoid doubling of suggestions menu, don't use again (for the 2nd time):

TextFields.bindAutoCompletion(..)
MaxKing
  • 188
  • 1
  • 8
1

In order to provide updates to the auto-complete suggestion list, retain a reference to the SuggestionProvider and update the suggestion provider instead:

TextField textField = new TextField();
SuggestionProvider suggestionProvider = SuggestionProvider.create(new ArrayList());
new AutoCompletionTextFieldBinding<>(textField, suggestionProvider);

When you want to update the suggestion list:

List<String> newSuggestions = new ArrayList();
//(add entries to list)
suggestionProvider.clearSuggestions();
suggestionProvider.addPossibleSuggestions(newSuggestions);
Iñigo
  • 1,877
  • 7
  • 25
  • 55
0

This will do the trick: Instead of: TextFields.bindAutoCompletion(textField, list); , try this:

List<String> strings = new ArrayList<>();

Then create binding between your textField with the list through:

new AutoCompletionTextFieldBinding<>(textField, SuggestionProvider.create(strings)); 

So any changes, including removing, from the list, will be reflected in the autoCompletion of the textField; And you will have dynamic filtering of suggestions, showed in pop-up, when user enter some text in textField;

MaxKing
  • 188
  • 1
  • 8
0

I had the same problem some time ago I try to do as @MaxKing mentions, but it didnt work. I managed to give it a solución even though I don't think it's the right way.

// Dispose the old binding and recreate a new binding

autoCompleteBinding.dispose();

autoCompleteBinding = TextFields.bindingAutoCompletion(TextField,List);

try this:

public void pushBills() {
    ArrayList list = new ArrayList<>();
    bills = new BillHeaderDao().FindAll();

    for (int i = 0; i < bills.size(); i++) {
        list.add(bills.get(i).getIdClient());
    }
    autoCompletionBinding.dispose();
    autoCompletionBinding = TextFields.bindAutoCompletion(SearchBill, SuggestionProvider.create(list));
}
Community
  • 1
  • 1