I want to display only the user id and the name in jList and when the user select something from the jList detailes of that person will be display in a lable

can you explain to me how to do it..
I've managed to display all the deatiles of the person i the JList , but I only want the id and name to display

3 Years
Discussion Span
Last Post by JamesCherrill

I've managed to display all the deatiles of the person i the JList , but I only want the id and name to display

The concept is the same, you override the toString() method for your JList. You've already changed what gets displayed in it, so I'm not sure where you're confused.


No, not toString for the JList. By default JList calls toString on each object that it displays to get some text to put in the list. You could change toString for your Person class, but that will probably mess up all the other places where it's used.
You could write a small list model that returns the desired String from you underlying list of Persons.
Or you could write a small cell renderer to draw justthe desired text.
Both these are described more in the intro to the JList API doc, but the simplest custom renderer just takes the default one and changes the text. eg

class CustomRenderer extends DefaultListCellRenderer {
    // same as DefaultListCellRenderer but uses custom text
    public Component getListCellRendererComponent(
            JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        // start with the default renderer, then replace its text..
        JLabel label = (JLabel) super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus);
        // cast the value to the appropriate class, and construct a suitable string
        label.setText(((Person) value).getName());
    return label;

ps: mKorbel has drawn my attention to the fact that this may not work too well with keyboard selection from the list using the first letter(s) of an entry - see the link in his post below for details and solutions.)

Edited by JamesCherrill

Votes + Comments

Yup. I guessed that Person has a toString that shows all its values, but for the JList only some of those values are required.

(Just for fun - in Java 8 I tried subclassing JList so you can pass a lambda to its constructor to define the mapping from the objects in the list to appropriate strings. It's really neat. I'm liking Java 8!)


Sorry I worded that wrong. By overriding the Person toString to display what she wants in the JList, you can remove the need for a custom renderer. (depending on how she actually wants it to appear) Then on the JList's mouse event, she can customize how the Person's data gets displayed in the label by ordering the values in a new string.

label.setText(person.getName()+" "+person.getAge());

Or simply configure that string structure inside the Person class. My initial thought was the person's details were being set in multiple labels.

I've been away from java for a few years, I'm a little rusty :)


@JamesCherrill Java8 as standard JDK/JRE???, will be here lots of fun, because as you see curriculum for Java programings homework is based by default on AWT with dinosaurs level, where we are living (own life)

Edited by mKorbel


Yes, Java 8 is scheduled for March 2014. Developer previews have been available for ages, along with a version of NetBeans that supports it, and the Release Candidate is due next week. The whole lambda thing is going to change the way we write a lot of code - no more anonymous inner classes for event listeners for a start.
You're right about homework - it will take years before teachers upate their syllabus and exercises, and corporations will take ages to update all their user base. That's their problem. I'm going to enjoy the latest and best for my work. ;)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.