..and I have an ArrayList of Person objects to populate a JList with
..but I only want to see in it (JList) the Person's Name, and to be
able -given a click- to retrieve the correct Person object from the JList
and to find it in the ArrayList.
What is the way to go to achieve this?. Any example would be appreciated!!
Rgds - jm
> I have a Person class who has the following instance variables:
> Name, Age, Height, Weight, date of birth, ..., ..., etc
>
> ..and I have an ArrayList of Person objects to populate a JList with
>
> ..but I only want to see in it (JList) the Person's Name,
You need a custom ListCellRenderer*,
it might be a JLabel as shown in the
JavaDocs for that class..
<http://www.physci.org/api.jsp?class=javax.swing.ListCellRenderer>
Then use it to JList.setCellRenderer(ListCellRenderer)
HTH
--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
>..but I only want to see in it (JList) the Person's Name, and to be
>able -given a click- to retrieve the correct Person object from the JList
>and to find it in the ArrayList.
>
>What is the way to go to achieve this?. Any example would be appreciated!!
ArrayLists are not designed for searching, though you could iterate
through the list looking at each person and comparing the name field
for a match.
You could do a binary search if you sort the names first.
If you can type the name exactly right, you can use a HashMap.
See http://mindprod.com/jgloss/hashtable.html
If you want to just type the approximate same (case insensitive) then
you want a TreeMap. You can then display the previous and next 10 and
let the user pick the exact match. See
http://mindprod.com/jgloss/Collection.html
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
> ..but I only want to see in it (JList) the Person's Name, and to be
> able -given a click- to retrieve the correct Person object from the JList
> and to find it in the ArrayList.
- Override Person.toString() to return the name
- Use the ArrayList in the JList's model
- When the user selects in the list (remember: not necessarily by
clicking), you get the selected index and it will map directly into
the ArrayList.
To customise the way JList displays its contents you need
javax.swing.ListCellRenderer , to listen for clicks on the JList you need
javax.swing.event.ListSelectionListener , and finally to extract the
currently selected item you need javax.swing.JList.getSelectedValue() .
Consult the Javadocs for details on all these.
-FISH- ><>
Since objects in a JList are by default visualized by displaying the
result of "toString()", you don't need your own cell renderer. Just
specify a toString() method in your Person class, which returns the
String that you want to see in the JList (the name).
Then implement a ListSelectionListener and add it to your JList, for
example like:
JList jl = new JList();
jl.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
Person p = (Person) jl.getSelectedValue();
// do with p what you want
}
});
Stefan
Yes, that occured to me - but I personally think that this is bad
practice. This is just my personal point of view, for the following
reasons: the objects being placed into the JList should, ideally, be
entirely independent of JList and its functionality. Using toString()
is a short-cut which reduces amount of code needed to display a list
of these objects - but at the expense of there no longer being a clear
separation between the container and its contents. Now, if the object's
toString() *just so happens* to return a string suitable for use as a
label in a JList, then that's all fine and dandy ... but the object
should not be specifically tailored to fit in with JList's functionality
(particularly a key method like toString(), which is used for more than
just list labels!)
Thinking back though, perhaps I was wrong to exclude this option alto-
gether. It might have been better to mention it, and add my concerns.
-FISH- ><>