Friday, June 29, 2007

HOWTO: remove multiple items from a JAVA Swing JList

Overview
When using the JList, there are times when you want to edit the list of items. Adding or removing a single item is pretty straight forward, however, there is a trick to removing groups of items from a JList.

For the following example, there is a simple JList that uses the DefaultListModel. The JList is setup with a MULTIPLE_INTERVAL selection model.

Technique
DefaultListModel dlm = (DefaultListModel) this.jList.getModel();

if(this.jList.getSelectedIndices().length > 0) {
int[] tmp = this.jList.getSelectedIndices();
int[] selectedIndices = this.jList.getSelectedIndices();

for (int i = tmp.length-1; i >=0; i--) {
selectedIndices = this.jList.getSelectedIndices();
dlm.removeElementAt(selectedIndices[i]);
} // end-for
} // end-if


Last Thoughts
This approach will let you grab any interval from a JList and remove them. Hopefully SUN will update this component to better handle this in the future. This will remove the need for a trick.

13 comments:

Anonymous said...

Oh my god!!!, so sorry, please so sorry, i used bad words in my last comment, but I was very desperated, because i can't erase multiple items for my JList, please so sorry, i used your source code on my code and Voila!!!, you're very good and master for the java code, Please accept my apologies because the estupid and idiot, i am. sorry Master

uuklanger said...

I removed the "bad words" from anon's comments. I too have used examples from sites that did not work on the first try. I am glad that this worked in the end.

Anonymous said...

For now i can't understand why, but it works :)
Thanks a lot.

Walia Himanshu said...

i am not able to understand the For loop ......which u have used
can u explain plz

Anonymous said...

i am not able to understand the For loop ......which u have used
can u explain plz

uuklanger said...

Based on a question from Anonymous December 18, 2008...

The main question was the for loop. Why was it done this way.

for (int i = tmp.length-1; i >=0; i--) {
selectedIndices = this.jList.getSelectedIndices();
dlm.removeElementAt(selectedIndices[i]);
} // end-for

Looking at it, you can see that it is starting with the last element and working up. Since the array length will always be one more then the max index value (Java uses 0 indexing in arrays), length-1 is used to get the last element in the array.

Removing the items in reverse order (MAX->0) is simpler the the other way (0->MAX). This is because the approach of using 0->MAX will always end up with a new array each time you remove the first element. Makes it a pain in a for loop. Counting backwards eliminates a "shift" in index values.

Before "plucking" the last element off the list, a call to getSelectedIndices() is done. This will let you know the element values for the remaining selected items. Next you just remove the last row from the model at the index location based on the item "i" is pointing to.

Something to try is to grab this code and put in some debug to watch the index and element values as it removes the rows. It may make it more clear.

Let me know if this clarifies.

GeekyCoder said...

Hi,
you might want to try this concise easy-to-understand solution too.

int[] _iArSelected = this.jList.getSelectedIndices()

int _a = 0;
for (int _i : _iArSelected)
{
dlm.removeElementAt(_i - _a++);
}

What happen is that for every item removal the next _i must decrement by removal count.

dudedatroz said...

yo man thanks alot i was stucked in this thing and ur coding worked fine for me, keep it up thnx.

Anonymous said...

you are the best omg... it's working like a charm!!

Anonymous said...

you are the best omg... it's working like a charm!!

Anonymous said...

you are the best omg... it's working like a charm!!

Anonymous said...

you are the best omg... it's working like a charm!!

Anonymous said...

Gracias amigo, justo lo que andaba buscando, funciona perfecto.

Saludos