using java version 9 I have some test code to remove a item from a list created by passing a refrence to by DefaultListModel. This is what I do.
- create a DefaultListModel object
- add 8 elements to it (A..H) by calling addElement
- remove a item by calling removeElement
- create a Jlist pass a reference of my DefaultListModel to it
The list box displays all 8 items, nothing got removed. code
philosophers = new DefaultListModel<String>(); philosophers.addElement( "A" ); philosophers.addElement( "B" ); philosophers.addElement( "C" ); philosophers.addElement( "D" ); philosophers.addElement( "E" ); philosophers.addElement( "F" ); philosophers.addElement( "G" ); philosophers.addElement( "H" ); philosophers.removeElement(1); lista = new JList<String>( philosophers );
When ever you have an issue, hit the JavaDocs...
DefaultListModel#removeElementThe interesting point here is, the parameter is an
Object, not a index. This means, with Java's auto-boxing, you're actually trying to remove aInteger(1), which does't exist in the model.Instead, if you did something like
philosophers.removeElement("B");, you might have had more luck.However, if we read a little more into the JavaDocs we find
DefaultListModel#removeAh, that sounds more like what you're after