0

I'm using Firebase with my Java project, I've added a listener to query data then add the elements to DefaultListModel but the JList only removes the old items and doesn't show the new data.
My JList

usersData = new DefaultListModel<>();
JList<String> users = new JList<>(usersData);
users.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
users.setSelectedIndex(0);
users.setVisibleRowCount(5);
JScrollPane fruitListScrollPane = new JScrollPane(users);
add(fruitListScrollPane, BorderLayout.SOUTH);

and my firebase value listener

firebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot snapshot) {
        // remove all old elements from DefaultListModel
        usersData.removeAllElements();
        // get the data from snapshot
        for (DataSnapshot userSnapshot : snapshot.getChildren()) {
            User user = userSnapshot.getValue(User.class);
            String userInfo = "Username: " + user.getUsername() + " & Password: " + user.getPassword();
            // add the info to the model
            usersData.addElement(userInfo);
            // print the info
            System.out.println(userInfo);
        }
    }

    @Override
    public void onCancelled(FirebaseError error) {
    }
});
SaNtoRiaN
  • 2,212
  • 2
  • 15
  • 25
  • So `usersData.removeAllElements()` is executed? But you then never enter the `for` loop? – Frank van Puffelen Mar 28 '16 at 17:44
  • No it enters and prints the `userInfo` String in the console, the problem is the `JList` doesn't get updated after I add new elements to the DefaultListModel `usersData` – SaNtoRiaN Mar 28 '16 at 17:49

2 Answers2

1

The problem is my listner's oonDataChange() runs in a thread so I used this code to do what I needed. I send the data list to this method then start adding them one by one.

public void addElements(final List<String> data) {
    if (SwingUtilities.isEventDispatchThread())
        return;
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            for (String item : data) {
                usersData.addElement(item);
                System.out.println(item);
            }
        }
    });
}

thanks to Refreshing GUI by another thread in java (swing)

Community
  • 1
  • 1
SaNtoRiaN
  • 2,212
  • 2
  • 15
  • 25
0

is usersData the right scope? I'm thinking it has to do with you updating the information in memory, but not on your UI elements. From the code youve given us, you dont assign or refresh the info in JList, just update the DefaultListModel<>. Try that

mewc
  • 1,253
  • 1
  • 15
  • 24
  • I read that `DefaultListModel.addElement()` updates the `JList`, doesn't it? – SaNtoRiaN Mar 28 '16 at 17:20
  • I believe it adds it to the array, but wouldn't change the UI element as itd be stuck on what it previously is on - hence your problem. – mewc Mar 30 '16 at 04:44
  • No, adding to the list model _does_ update the UI. The UI registers itself with the model's `addListDataListener` for exactly this reason. – Ti Strga Mar 06 '20 at 22:32