I need some advice on how to implement some things with Swing. I'll use three examples:
I've implemented a file tree. I get the directory contents in the model when getChildCount is called. getChildCount starts a thread to not freeze de interface and get the minimum data needed to sort the children putting the directories first. Then I start a new thread in the model to get more data about the files like the content type to be use in the renderer to show the associated icon (this improves the perceived load speed of the directory).
In a similar way I add items to a custom table model for audio items and then I start a thread in the model to get the files metadata (author, title, length, ...)
I'm going to develop a YouTube player and I'll use a JList to show a list of results when you make a search. I want to add a thumbnail for every list item obtained from the url provided in the search result page.
What I wanted to know is if it is sane doing things like these in the model or if it would be better to take another approach like getting the data in external to the model threads and then invoking the methods (add, remove, value changed, etc.) in the model. As done right now is very comfortable, but I read frequently that the model only should store the data and nothing more.
I would do all that background loading in a class or classes whose only connection with Swing would be to hold a SwingPropertyChangeSupport as a private member, to which it/they forward add/removePropertyChangeListener(...) calls. Then fire a property change whenever there is new data that should be updated in the view.
The propertyChange(...) method in the listener(s) would retrieve the new data and update the view.
Disclaimer: I'm not a professional so there may be a better way to go about this.
There are no new questions, but there may be new answers.
Joined: Nov 23, 2012
I'm not an expert either, just doing this for fun. I don't quite understand what you mean, but I'll investigate about that to compare it with my current way of doing stuff like these. Thanks for the hint.