hego64
hego64

Reputation: 365

Objectdb - dynamic search bar

I'm trying to have a dynamic search bar in which you can type and it will update a ListView with the results of the search. What I have so far is a listener on the TextField and a query set to run each time the search bar is modified:

   search.textProperty().addListener((observable, oldValue, newValue) -> {
        TypedQuery<Skill> query = em.createQuery
        ("SELECT p FROM Skill p WHERE p.skill_name LIKE '%" + newValue + "%'", Skill.class);

        lv.getItems().clear();
        skill_list.clear();

        skill_list.addAll(query.getResultList());

        lv.getItems().addAll(skill_list);

    });

The issue with this is that, once any match has been found that is less than the total number of skills in the database (currently 3 for testing), it always displays three skills in the ListView, never any less despite clearing everything and making sure that the results of the query is also just 1. It does find the match, however, and puts it at the top of the ListView without removing any extras which are not even in the queries resultList.

I've tried using separate lists for storing the results and the skills that populate the list before the search bar is used, which changes nothing. How can I make this dynamic search function correctly?

Edit: I am using a CellFactory to display the data in the ListView. That code is as follows:

lv.setCellFactory(param -> new ListCell<Skill>() {
        @Override
        protected void updateItem(Skill skill, boolean empty) {
            super.updateItem(skill, empty);
            if (!empty && skill != null) {
                setText(skill.getName());
                setStyle(null);
            } 
        }
    });

Upvotes: 0

Views: 172

Answers (0)

Related Questions