Advice needed

Discussion in 'Java' started by Dirk Bruere at NeoPax, Jul 10, 2009.

  1. Dirk Bruere at NeoPax, Jul 10, 2009
    #1
    1. Advertisements

  2. Dirk Bruere at NeoPax

    markspace Guest

    Drop both album name and path in a single object. Make a comparator
    that sorts on name only, and sort 'em. Drop that into the first JList,
    and make the second JList by pulling the names out, in the order given
    by the first sort, and adding them to the second JList.

    You could also do tricky things like making more than one Model for the
    list, one that pulls names out and shows them to the JList, and one that
    pulls out albums. Keep the original list in sorted order, as a kind of
    backing array for both lists. If you need to add or remove an object,
    you can keep both lists in synch this just by updating the backing array
    and allowing the JLists to update themselves.
     
    markspace, Jul 10, 2009
    #2
    1. Advertisements

  3. My gut reaction is to make a map from the name to the index. Another
    possibility is to make the object that represents the name have the path
    as well, which may require custom rendering.
     
    Joshua Cranmer, Jul 10, 2009
    #3
  4. Dirk Bruere at NeoPax

    Daniel Pitts Guest

    Yes, have one ListModel, that contains an Album object which has both
    name and path in it. Now, you can sort the ListModel however you want,
    and both JLists will have the correct data.

    You will need to create your own ListCellRender classes to pull out the
    appropriate field from your Album object.
     
    Daniel Pitts, Jul 11, 2009
    #4
  5. I think that's probably the only solution.

    --
    Dirk

    http://www.transcendence.me.uk/ - Transcendence UK
    http://www.theconsensus.org/ - A UK political party
    http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
     
    Dirk Bruere at NeoPax, Jul 11, 2009
    #5
  6. Dirk Bruere at NeoPax

    Roedy Green Guest

    Create a Album class that has fields for name and path. Then you have
    one ArrayList you can sort. see http://mindprod.com/jgloss/sort.html
    Then you peel off the data you need for your JLists.

    You can even use the original ArrayList for the JList of album names,
    by defining toString() appropriately.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "For reason that have a lot to do with US Government bureaucracy, we settled on the one issue everyone could agree on, which was weapons of mass destruction."
    ~ Paul Wolfowitz 2003-06, explaining how the Bush administration sold the Iraq war to a gullible public.
     
    Roedy Green, Jul 12, 2009
    #6
  7. Dirk Bruere at NeoPax

    Roedy Green Guest

    This is what a newbie might do, intimidated by custom renderers. The
    grown-up way is to write a ListCellRenderer to select the field.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "For reason that have a lot to do with US Government bureaucracy, we settled on the one issue everyone could agree on, which was weapons of mass destruction."
    ~ Paul Wolfowitz 2003-06, explaining how the Bush administration sold the Iraq war to a gullible public.
     
    Roedy Green, Jul 12, 2009
    #7
  8. Sounds better suited to a JTable (which can also do
    sorting and filtering).
     
    Andrew Thompson, Jul 12, 2009
    #8
  9. Dirk Bruere at NeoPax

    Daniel Pitts Guest

    That depends, JTable has a specific UI, which may not actually match
    what the OP needs/wants. It does seem likely, however, that a JTable
    could be suitable, if the UI matches the needs.
     
    Daniel Pitts, Jul 13, 2009
    #9
  10. What I actually did:

    private void sortAlbumLists()
    {
    int size = albumListModel.getSize(), index;
    String[] arrayStr = new String[size];
    String myStr, nameStr, pathStr;

    for ( int i=0; i<size; i++)
    {
    myStr = (String) albumListModel.getElementAt(i) +
    ";;" + (String) albumPathListModel.getElementAt(i);
    arrayStr = myStr;
    }

    Arrays.sort(arrayStr);

    albumListModel.clear();
    albumPathListModel.clear();

    for ( int i=0; i<size; i++)
    {
    myStr = arrayStr;

    index = myStr.indexOf(";;"); //Find
    out where the double comma is located
    pathStr = myStr.substring(index+2);
    nameStr = myStr.substring(0, index);

    albumListModel.addElement(nameStr);
    albumPathListModel.addElement(pathStr);
    }
    }

    The ";;" was used because I am using an API that also uses that to
    separate text fields in the transmission protocol

    --
    Dirk

    http://www.transcendence.me.uk/ - Transcendence UK
    http://www.theconsensus.org/ - A UK political party
    http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
     
    Dirk Bruere at NeoPax, Jul 13, 2009
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.