aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Doubts with design Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Doubts with design" Watch "Doubts with design" New topic
Author

Doubts with design

Johan Carson
Greenhorn

Joined: Nov 23, 2012
Posts: 6
Hi.

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.

    Thanks in advance.

    Greetings.
    Darryl Burke
    Bartender

    Joined: May 03, 2008
    Posts: 4540
        
        5

    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.


    luck, db
    There are no new questions, but there may be new answers.
    Johan Carson
    Greenhorn

    Joined: Nov 23, 2012
    Posts: 6
    Hi.

    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.

    Greetings.
    Darryl Burke
    Bartender

    Joined: May 03, 2008
    Posts: 4540
        
        5

    Basically, something like this (typed here, may have typos or other absurdities)
    Johan Carson
    Greenhorn

    Joined: Nov 23, 2012
    Posts: 6
    Hi.

    OK, I now understand it. I knew PropertyChangeSupport but not SwingPropertyChangeSupport and it's an interesting class because it fires the events in the EDT. Thank you again.

    Greetings.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Doubts with design