The default model for a list does not allow adding and removing items. The list must be created with a DefaultListModel component, as shown in the following snippet: //you can create a custom list models or use DefaultListModel DefaultListModel model = new DefaultListModel(); JList myList = new JList(model); //allows adds and removes // load myList with items model.add(0, "one"); model.add(1, "two"); model.add(2, "three"); myList.setForeground(Color.blue); myList.setBackground(Color.white); // this is how you insert model.add(0, "zero"); // this is how you append model.add(myList.getModel().getSize(), "four"); You might consider using a list or combination box to present choices to the user. This is a good idea for your project because it reduces the chance of user errors, which are inevitable when user responses are taken from typed input. |