File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JList" Watch "JList" New topic


Peter Phung
Ranch Hand

Joined: Dec 06, 2001
Posts: 138
please can anyone tell me how i can add a single item/object to a JList?

Pete<br />"Reality is an illusion <br />brought on by a lack of <br />drink, drugs and smut"
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
Hi Pete,
you can try this

Instead of a vector, you can also use any Object[].
Hope this helps
Shane Kirk

Joined: Jul 19, 2002
Posts: 5
Rene's method works...but there's another way that might involve less overhead if you have a lot of insertions/deletions occuring in the JList data.
The JList's list model is a DefaultListModel. So, you could do something like the following.
DefaultListModel model = (DefaultListModel) myList.getModel();
I can't remember off the top of my head if this fires an event for the JList to redraw itself or not. If not, a simple call to the JList's repaint method should probably suffice.
[ July 22, 2002: Message edited by: Shane Kirk ]
I agree. Here's the link:
subject: JList
It's not a secret anymore!