Last active
March 5, 2019 01:48
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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