Skip to content

Instantly share code, notes, and snippets.

@briandk
Last active March 5, 2019 01:48
Show Gist options
  • Save briandk/d2846c39a52503a9238d9b6ae344b13e to your computer and use it in GitHub Desktop.
Save briandk/d2846c39a52503a9238d9b6ae344b13e to your computer and use it in GitHub Desktop.
Editing a JList in Java. This code repeatedly adds the user “Krang” to a list every time the button is pressed.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultList newList = new DefaultListModel();
ListModel oldList = (ListModel) myList.getModel();
// Copy over the old list
for (int i = 0; i < oldList.getSize(); i++) {;
newList.add(i, oldList.getElementAt(i));
}
newList.add(0, "Krang");
myList.setModel(newList);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment