@Test public void testSetSelectedItems() { final List<String> selected = new ArrayList<String>(); DefaultListModel model = new DefaultListModel(); final String[] listItemStrings = new String[] {"test1", "test2", "test3"}; for(String item: listItemStrings) model.addElement(item); list.setModel(model); selected.add("test2"); list.setSelectedItems(selected); Object[] selectedVals = list.getSelectedValues(); assertEquals(1, selectedVals.length); selected.clear(); for(String item: listItemStrings) selected.add(item); list.setSelectedItems(selected); selectedVals = list.getSelectedValues(); assertEquals(3, selectedVals.length); selected.clear(); selected.add("invalid"); list.setSelectedItems(selected); selectedVals = list.getSelectedValues(); assertEquals(0, selectedVals.length); }
public void setSelectedItems(final List<String> selected) { final ListSelectionListener[] listeners = this.getListSelectionListeners(); for(ListSelectionListener l :listeners) removeListSelectionListener(l); getSelectionModel().clearSelection(); selectionCache.clear(); final int size = this.getModel().getSize(); for(int i=0; i<size; i++) { if(selected.contains(getModel().getElementAt(i))) { getSelectionModel().addSelectionInterval(i, i); selectionCache.add(i); } } for(ListSelectionListener l :listeners) addListSelectionListener(l); }
CheckBoxJList extends JList implements ListSelectionListener { public void setSelectedItems(final List<String> selected) { final ListSelectionListener[] listeners = this.getListSelectionListeners(); for(ListSelectionListener l :listeners) removeListSelectionListener(l); getSelectionModel().clearSelection(); selectionCache.clear(); final int size = this.getModel().getSize(); for(int i=0; i<size; i++) { if(selected.contains(getModel().getElementAt(i))) { getSelectionModel().addSelectionInterval(i, i); selectionCache.add(i); } } for(ListSelectionListener l :listeners) addListSelectionListener(l); } }
CheckBoxJList extends JList implements ListSelectionListener { public void setSelectedItems(final List<String> selected) { final ListSelectionListener[] listeners = this.getListSelectionListeners(); for(ListSelectionListener l :listeners) removeListSelectionListener(l); getSelectionModel().clearSelection(); selectionCache.clear(); final int size = this.getModel().getSize(); for(int i=0; i<size; i++) { if(selected.contains(getModel().getElementAt(i))) { getSelectionModel().addSelectionInterval(i, i); selectionCache.add(i); } } for(ListSelectionListener l :listeners) addListSelectionListener(l); } @SuppressWarnings("unchecked")  CheckBoxJList(); }
CheckBoxJList extends JList implements ListSelectionListener { public void setSelectedItems(final List<String> selected) { final ListSelectionListener[] listeners = this.getListSelectionListeners(); for(ListSelectionListener l :listeners) removeListSelectionListener(l); getSelectionModel().clearSelection(); selectionCache.clear(); final int size = this.getModel().getSize(); for(int i=0; i<size; i++) { if(selected.contains(getModel().getElementAt(i))) { getSelectionModel().addSelectionInterval(i, i); selectionCache.add(i); } } for(ListSelectionListener l :listeners) addListSelectionListener(l); } @SuppressWarnings("unchecked")  CheckBoxJList(); void setSelectedItems(final List<String> selected); @Override void valueChanged(ListSelectionEvent lse); }
CheckBoxJList extends JList implements ListSelectionListener { public void setSelectedItems(final List<String> selected) { final ListSelectionListener[] listeners = this.getListSelectionListeners(); for(ListSelectionListener l :listeners) removeListSelectionListener(l); getSelectionModel().clearSelection(); selectionCache.clear(); final int size = this.getModel().getSize(); for(int i=0; i<size; i++) { if(selected.contains(getModel().getElementAt(i))) { getSelectionModel().addSelectionInterval(i, i); selectionCache.add(i); } } for(ListSelectionListener l :listeners) addListSelectionListener(l); } @SuppressWarnings("unchecked")  CheckBoxJList(); void setSelectedItems(final List<String> selected); @Override void valueChanged(ListSelectionEvent lse); static final String LIST_UPDATED; }