jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Clearing a JList" Watch "Clearing a JList" New topic

Clearing a JList

Carlos Delgado

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:
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...

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

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
It's not a secret anymore!