listModel = new DefaultListModel(); listModel.addElement("Alison Huml"); listModel.addElement("Kathy Walrath"); listModel.addElement("Lisa Friendly"); listModel.addElement("Mary Campione"); //Create the list and put it in a scroll pane list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setSelectedIndex(0);int index = list.getSelectedIndex(); listModel.remove(index);
yes, use the ListModel to manage the data and just delegate the UI to the JList. That's a very good design in Swing components, although sometimes it may seem to cause little troubles.
所有的增减都是对Vector进行
listModel.addElement("Alison Huml");
listModel.addElement("Kathy Walrath");
listModel.addElement("Lisa Friendly");
listModel.addElement("Mary Campione"); //Create the list and put it in a scroll pane
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);int index = list.getSelectedIndex();
listModel.remove(index);
DefaultListModel model=new DefaultListModel();
JList list=new JList();
model.addElement("aaaaa");
model.addElement("bbbbb");//JList列表框中的列表内容
list.setModel(model);OK
删除就用removeElement(Object element);
addElement(),是添加,
removeElement(list.getSelectedValue())是删除
list.getModel().remove(index);