1

this confuses me. i add objects in JList , like below:

public class RequestListModel extends AbstractListModel<Request> {

private static final long serialVersionUID = 1L;
private List<Request> data = null;

public RequestListModel (List<Request> data) {
    this.data = data;
}

@Override
public int getSize() {
    return this.data.size();
}

@Override
public Request getElementAt(int index) {
    Request request = data.get(index);
    return request;
}}

private JList<Request> getList() {
    ListModel<Request> model = new RequestListModel(this.requestList);
    if(jlist_from == null) {
        jlist_from = new JList<Request>(model);
    } else {
        jlist_from.setModel(model);
    }
    return jlist_from;
}

but when i run the program, it just shows the object's address : enter image description here

so how would i show the text from the object ? thank you very much.

4dc0
  • 453
  • 3
  • 11
Brave ru
  • 61
  • 7

1 Answers1

1

it just show the object's address

The default renderer for a JList simply invokes the toString() method of the object, which by default is the objects address.

You should provide a custom renderer for your JList. The render allows you to access the object and display any data from the object in any format that you wish. Read the section from the Swing tutorial on Using a Custom Renderer.

A simpler solution is to implement a custom toString() method in your object. Although this approach is not recommended since the toString() should be used to describe the object when debugging.

Also, there is no reason to create a custom ListModel. You can just use the DefaultListModel to hold your Request objects.

camickr
  • 321,443
  • 19
  • 166
  • 288