Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update autoComplete JavaFx?

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

like image 411
Menai Ala Eddine - Aladdin Avatar asked Aug 20 '17 04:08

Menai Ala Eddine - Aladdin


2 Answers

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(..)
like image 195
MaxKing Avatar answered Sep 25 '22 20:09

MaxKing


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);
like image 28
Dennis Sheirer Avatar answered Sep 26 '22 20:09

Dennis Sheirer