Is it possible to change the names *shown* for the items in a Java Swing jList?
Asked Answered
D

2

11

I have a jList that uses DefaultListModel and I populate it with Objects I get from a list (context: the objects are a type of agent in ABM system).

Is it possible to change the name that is shown for the objects in the jList? I haven't been able to find anything on this...

Dipeptide answered 23/12, 2010 at 12:8 Comment(0)
C
10

If the information you want to see (instead of whatever toString() spits out) is contained in the object itself, the most direct "Swing" way to accomplish this is through the use of a ListCellRenderer. Think of a ListCellRenderer (any CellRenderer really) as a rubber stamp used to draw each object in your list. The object is passed in, you setup the component, the component draws your object, and then moves on to the next object. The CellRenderer never has any state.

Consider this example:

// Extend DefaultListCellRenderer, takes care of most of the work for you
public class ExampleListCellRenderer extends DefaultListCellRenderer
{
    public Component getListCellRendererComponent(
        JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
        // I know DefaultListCellRenderer always returns a JLabel
        // super setups up all the defaults
        JLabel label = (JLabel)super.getListCellRendererComponent(list, value, index, isSelect, cellHasFocus);

        // "value" is whatever object you put into the list, you can use it however you want here

        // I'm going to prefix the label text to demonstrate the point

       label.setText("PRE:" + label.getText());

       return label;

    }
}

// Some time later...

JList list = new JList();
list.setCellRenderer(new ExampleListCellRenderer());
Cyrillus answered 23/12, 2010 at 12:34 Comment(4)
basszero thanks for your detailed and informative explanation of listcellrenderers and the example you provided. I learned a lot :)Dipeptide
@Dipeptide - check the box next to the answer to accept it and give @Cyrillus the points s/he deserves.Quod
@i82Much both answers I have received are correct. I can't indicate that in any way?Dipeptide
Unfortunately no, other than to upvote both. An upvote gives the answerer points, but not as many as when you accept their answer.Quod
G
4

I think the names are produced by the toString() methods of those objects. If you can change that, that's easiest. Otherwise, a quick solution would be to wrap some kind of holder object around each one that generates the view of the object for the JList and from which you can retrieve the contained object easily enough when you have to manipulate it for real.

To expand on the wrapper concept:

class FooBarAgentHolder {
    // Simple javabean stuff
    private FooBarAgent agent;
    public FooBarAgentHolder(FooBarAgent agent) { this.agent = agent; }
    public FooBarAgent getAgent() { return agent; }

    // Produce the name for human consumption
    public String toString() {
        return agent.getDescriptiveName(); // Or whatever...
    }

    // Convenience conversion function
    public static DefaultListModel makeListModel(List<FooBarAgent> list) {
        DefaultListModel result = new DefaultListModel();
        for (FooBarAgent agent: list)
            result.addElement(new FooBarAgentHolder(agent));
        return result;
    }
}
Grimaldo answered 23/12, 2010 at 12:11 Comment(2)
Thanks for your prompt reply. I don't want to change the original objects name, so i guess that option is out of question, unless there are some tricks that i am not aware of. On your second suggestion. Could you kindly expand on it? I am completely naive about this. an example would be extremely helpful...Dipeptide
@blackace: Try that. It should be simple enough code that you can see it's obviously correct. Adapt to fit your actual code (e.g., change FooBarAgent and do the right thing in toString).Grimaldo

© 2022 - 2024 — McMap. All rights reserved.