Re: how to delete a JList element
On Sat, 21 Jun 2008 02:51:41 +0000, Roedy Green wrote:
ListModel does not have any sort of remove method.
However DefaultListModel does. If your model were a DefaultListModel
then you could cast to (DefaultListModel) or simply hold onto a
reference to it and use its removeElementAt method.
This seems to give a NullPointerException, as below, because the backing
collection is a DefaultListModel:
java.lang.NullPointerException
at a00720398.view.NewJFrame.displayGuest(NewJFrame.java:458)
at a00720398.view.NewJFrame.showGuest(NewJFrame.java:435)
at a00720398.view.NewJFrame.showGuest(NewJFrame.java:486)
at a00720398.view.NewJFrame.access$000(NewJFrame.java:13)
at a00720398.view.NewJFrame$1.valueChanged(NewJFrame.java:332)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1765)
...
thufir@arrakis:~/bcit-comp2611-project2$
thufir@arrakis:~/bcit-comp2611-project2$ head src/a00720398/view/
NewJFrame.java -n 481 | tail -n 10
private void deleteGuestActionPerformed(java.awt.event.ActionEvent
evt)//GEN-FIRST:event_deleteGuestActionPerformed
{//GEN-HEADEREND:event_deleteGuestActionPerformed
int i =guestList.getSelectedIndex();
BedAndBreakfast bnb = BedAndBreakfast.getInstance(); //the
"database"
System.out.println("delete guest \t" + i + bnb.getGuests().get(i));
((DefaultListModel)(guestList.getModel())).removeElementAt(i);
System.out.println("bnb\n" + bnb.toString());
}//GEN-LAST:event_deleteGuestActionPerformed
thufir@arrakis:~/bcit-comp2611-project2$
So, I think that I'll have to change BedAndBreakfast.getGuests() not to
return a DefaultListModel, which I assume is causing the
NullPointerException, but rather have it return, for instance, a Vector.
yes/no?
thanks,
Thufir