Target Audience: Java Beginners, Java UI Developers, Swing Developers
What should you know already? Java Swing API
When you work around Java Swing UI components, especially JList, JTable or JComboBox, you should give your attention more than the attention you are giving for other components. Because these components are not just a single control, it has 0 or more elements in it. So, adding or deleting elements should be done in a meaningful way.
This blog gives you a very useful tip about delete elements from JList which has multiple selection mode. First look at the following code, the typical way how elements can be added using DefaultListModel:
String fruits[]={"Grapes","Guaua","Pine Apple","Mango","Strawberry","Banana", "Watermelon","Apple","Lemon","Muskmelon","Orange"}; final JList lstFruits=new JList(); final DefaultListModel listModel=new DefaultListModel(); for(String fruit:fruits) listModel.addElement(fruit); lstFruits.setModel(listModel); lstFruits.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
lstFruits list contains 11 fruits. And create a JButton and bind ActionEvent for writing delete elements logic, as look as following code:
JButton btnDelete=new JButton("Delete"); btnDelete.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { int [] selectedIndices=lstFruits.getSelectedIndices(); for(int index:selectedIndices) listModel.removeElementAt(index); } } );
Now, create objects for JFrame and JPanel. Add lstFruits and btnDelete to JPanel object. Execute the code. Select single element and press Delete button. The selected element would be deleted. It works fine. However, select multiple elements of the list box (not essestially as sequence elements, select multiple interval selection) and press on Delete button. Now you could notice not all of the selected elements would be deleted. Don’t forget to check your console output for the exception thrown which might be likely as follows:
Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: 9 >= 6
at java.util.Vector.removeElementAt(Vector.java:511)
at javax.swing.DefaultListModel.removeElementAt(DefaultListModel.java:312)
What is the reason?
You could come to know the reason for this exception if you analyze the delete logic. We are deleting the selected elements using a loop. So it deletes one element in one iteration. Note, when it deletes an element from the list, Java updates the index of existing elements in the list. So, in the next iteration, if you are trying to delete an element by its selected index, the element might not be available at the selected index, since Java had updated the index values in the last iteration. So, its position within the list is changed.
How to solve?
Delete the element, whose index is biggest, first. If you follow this tip, no chance for the exception. The trick is start iteration in reverse order. Check the following code:
btnDelete.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { int [] selectedIndices=lstFruits.getSelectedIndices(); int length=selectedIndices.length; for (int index = length - 1; index >= 0; index--) listModel.removeElementAt(selectedIndices[index]); } } );
The above logic neatly deletes the selected elements from JList without any exceptions. You can use the same kind of trick for JTable also.
Feel free to send comments. About Author