Advice needed

  • Thread starter Dirk Bruere at NeoPax
  • Start date
D

Dirk Bruere at NeoPax

M

markspace

Dirk said:
I have two JLists, one containing album names and another containing the
corresponding album paths.

I want to sort the names into alphabetical order (no problem), but also
keep the paths in corresponding order so that an index of name also
delivers the path.

Any ideas as the best way to do it?

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.
 
J

Joshua Cranmer

Dirk said:
I have two JLists, one containing album names and another containing the
corresponding album paths.

I want to sort the names into alphabetical order (no problem), but also
keep the paths in corresponding order so that an index of name also
delivers the path.

Any ideas as the best way to do it?

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.
 
D

Daniel Pitts

Dirk said:
I have two JLists, one containing album names and another containing the
corresponding album paths.

I want to sort the names into alphabetical order (no problem), but also
keep the paths in corresponding order so that an index of name also
delivers the path.

Any ideas as the best way to do it?
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.
 
D

Dirk Bruere at NeoPax

Daniel said:
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.

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
 
R

Roedy Green

I have two JLists, one containing album names and another containing the
corresponding album paths.

I want to sort the names into alphabetical order (no problem), but also
keep the paths in corresponding order so that an index of name also
delivers the path.

Any ideas as the best way to do it?

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.
 
R

Roedy Green

Then you peel off the data you need for your JLists.

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.
 
A

Andrew Thompson

I have two JLists, one containing album names and another containing the
  corresponding album paths.

Sounds better suited to a JTable (which can also do
sorting and filtering).
 
D

Daniel Pitts

Andrew said:
Sounds better suited to a JTable (which can also do
sorting and filtering).
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.
 
D

Dirk Bruere at NeoPax

Roedy said:
This is what a newbie might do, intimidated by custom renderers. The
grown-up way is to write a ListCellRenderer to select the field.

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Official Java Classes 10
Can an Applet beep? 4
File over network timeout 3
Free keyboard applet 5
JMF? 21
Delay 2
Change character in string 105
Java in Browser 57

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top