Mega Code Archive

 
Categories / Java Tutorial / Swing
 

Adding and Removing an Item in a JList Component

import javax.swing.DefaultListModel; import javax.swing.JList; public class Main {   public static void main(String[] argv) throws Exception {     DefaultListModel model = new DefaultListModel();     JList list = new JList(model);     // Initialize the list with items     String[] items = { "A", "B", "C", "D" };     for (int i = 0; i < items.length; i++) {       model.add(i, items[i]);     }   } }