File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Clearing a JList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Clearing a JList" Watch "Clearing a JList" New topic
Author

Clearing a JList

Carlos Delgado
Greenhorn

Joined: Aug 17, 2004
Posts: 15
Hi all!
I want to clear all the items of the list because depending on the option the user chooses I'm showing one kind of information or another.
I'm trying list.removeAll(). It doesn't work.
With the DefaultListModel associated to the JList I'm trying:
listModel.clear();
listModel.removeAllElements();
Even I'm trying to delete each element manually with listModel.removeElementAt(index);
These 3 statements crash if one element of the list is selected. I want to know if there's a way of clearing the list or deselecting the selected item...

Thanks!
Pat Hays
Ranch Hand

Joined: Aug 20, 2004
Posts: 138
Code is here

jList1.setListData(new Object[0]);
[ August 20, 2004: Message edited by: Pat Hays ]

Download Java GUI Builder, <a href="http://www.mars3000.com" target="_blank" rel="nofollow">http://www.mars3000.com</a>
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
listModel.clear() works OK to remoave all elements
list.clearSelection() is the other one you need

try this

Carlos Delgado
Greenhorn

Joined: Aug 17, 2004
Posts: 15
Thanks for replying! I actually got it working now!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clearing a JList