Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Display arraylist using DefaultListModel

794404Apr 28 2009 — edited Apr 29 2009
Hi,

I've created a DefaultListModel for my arraylist of objects. In the object class, I have overidden the toString method with the fotmat specifiers and I have given minimum field length for each fields to be displayed.

e.g. return String.format("\n%-30s%-15s%-6s%",getFirstName(),getLastName(), getAge().....

I have a spererate class where I created my DefaultListModel. The code is:

public class Frame extends JFrame {

public Frame(ArrayList<People> list){

listModel = new DefaultListModel();

for (int i = 0; i < list.size(); i++)
{
listModel.addElement(list.get(i));
}
Container cn = this.getContentPane();
JList modelList = new JList(listModel);
cn.setLayout(new BorderLayout());
cn.add(new JScrollPane(modelList),BorderLayout.CENTER);
this.setSize(600,200);

}
}

when I execute, it displays the array in the JFrame, but does not give the minimum field width correctly for each field in my Movie class. (toString method)

The output is below. As you can see, even though I gave 30(minimum field width) for the firstName, it does not display it accordingly. But when I run this in the command prompt, I don't get this problem.

Samuell Antonny
Margerette Marshall

Appreciate if someone can give me a hint. Thanks for any help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 27 2009
Added on Apr 28 2009
12 comments
4,262 views