String[] data = {"item 1", "item 2", "item 3"}; JListlist = new JList<>(data); ListModel model = list.getModel();
DefaultListModelIn this example, we create a DefaultListModel and add some items to it. We then create a JList and initialize it with the DefaultListModel. These examples use the javax.swing package library.model = new DefaultListModel<>(); model.addElement("item 1"); model.addElement("item 2"); model.addElement("item 3"); JList list = new JList<>(model);