@Test public void testValueChanged() { 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); for(String item: listItemStrings) selected.add(item); list.setSelectedItems(selected); ListSelectionEvent lse = mock(ListSelectionEvent.class); when(lse.getValueIsAdjusting()).thenReturn(false); list.valueChanged(lse); verify(lse, times(1)).getValueIsAdjusting(); }
@Override public void valueChanged(ListSelectionEvent lse) { if (!lse.getValueIsAdjusting()) { firstIndex = lse.getFirstIndex(); lastIndex = lse.getLastIndex(); removeListSelectionListener(this); final HashSet<Integer> newSelections = new HashSet<Integer>(); final int size = getModel().getSize(); final ListSelectionModel select = getSelectionModel(); for (int i = 0; i < size; i++) { if (select.isSelectedIndex(i)) newSelections.add(i); } for (Integer index : selectionCache) { select.addSelectionInterval(index, index); } for (Integer index : newSelections) { if (selectionCache.contains(index)) select.removeSelectionInterval(index, index); else select.addSelectionInterval(index, index); } selectionCache.clear(); for (int i = 0; i < size; i++) { if (select.isSelectedIndex(i)) selectionCache.add(i); } addListSelectionListener(this); firePropertyChange(LIST_UPDATED, null, null); } }
CheckBoxJList extends JList implements ListSelectionListener { @Override public void valueChanged(ListSelectionEvent lse) { if (!lse.getValueIsAdjusting()) { firstIndex = lse.getFirstIndex(); lastIndex = lse.getLastIndex(); removeListSelectionListener(this); final HashSet<Integer> newSelections = new HashSet<Integer>(); final int size = getModel().getSize(); final ListSelectionModel select = getSelectionModel(); for (int i = 0; i < size; i++) { if (select.isSelectedIndex(i)) newSelections.add(i); } for (Integer index : selectionCache) { select.addSelectionInterval(index, index); } for (Integer index : newSelections) { if (selectionCache.contains(index)) select.removeSelectionInterval(index, index); else select.addSelectionInterval(index, index); } selectionCache.clear(); for (int i = 0; i < size; i++) { if (select.isSelectedIndex(i)) selectionCache.add(i); } addListSelectionListener(this); firePropertyChange(LIST_UPDATED, null, null); } } }
CheckBoxJList extends JList implements ListSelectionListener { @Override public void valueChanged(ListSelectionEvent lse) { if (!lse.getValueIsAdjusting()) { firstIndex = lse.getFirstIndex(); lastIndex = lse.getLastIndex(); removeListSelectionListener(this); final HashSet<Integer> newSelections = new HashSet<Integer>(); final int size = getModel().getSize(); final ListSelectionModel select = getSelectionModel(); for (int i = 0; i < size; i++) { if (select.isSelectedIndex(i)) newSelections.add(i); } for (Integer index : selectionCache) { select.addSelectionInterval(index, index); } for (Integer index : newSelections) { if (selectionCache.contains(index)) select.removeSelectionInterval(index, index); else select.addSelectionInterval(index, index); } selectionCache.clear(); for (int i = 0; i < size; i++) { if (select.isSelectedIndex(i)) selectionCache.add(i); } addListSelectionListener(this); firePropertyChange(LIST_UPDATED, null, null); } } @SuppressWarnings("unchecked")  CheckBoxJList(); }
CheckBoxJList extends JList implements ListSelectionListener { @Override public void valueChanged(ListSelectionEvent lse) { if (!lse.getValueIsAdjusting()) { firstIndex = lse.getFirstIndex(); lastIndex = lse.getLastIndex(); removeListSelectionListener(this); final HashSet<Integer> newSelections = new HashSet<Integer>(); final int size = getModel().getSize(); final ListSelectionModel select = getSelectionModel(); for (int i = 0; i < size; i++) { if (select.isSelectedIndex(i)) newSelections.add(i); } for (Integer index : selectionCache) { select.addSelectionInterval(index, index); } for (Integer index : newSelections) { if (selectionCache.contains(index)) select.removeSelectionInterval(index, index); else select.addSelectionInterval(index, index); } selectionCache.clear(); for (int i = 0; i < size; i++) { if (select.isSelectedIndex(i)) selectionCache.add(i); } addListSelectionListener(this); firePropertyChange(LIST_UPDATED, null, null); } } @SuppressWarnings("unchecked")  CheckBoxJList(); void setSelectedItems(final List<String> selected); @Override void valueChanged(ListSelectionEvent lse); }
CheckBoxJList extends JList implements ListSelectionListener { @Override public void valueChanged(ListSelectionEvent lse) { if (!lse.getValueIsAdjusting()) { firstIndex = lse.getFirstIndex(); lastIndex = lse.getLastIndex(); removeListSelectionListener(this); final HashSet<Integer> newSelections = new HashSet<Integer>(); final int size = getModel().getSize(); final ListSelectionModel select = getSelectionModel(); for (int i = 0; i < size; i++) { if (select.isSelectedIndex(i)) newSelections.add(i); } for (Integer index : selectionCache) { select.addSelectionInterval(index, index); } for (Integer index : newSelections) { if (selectionCache.contains(index)) select.removeSelectionInterval(index, index); else select.addSelectionInterval(index, index); } selectionCache.clear(); for (int i = 0; i < size; i++) { if (select.isSelectedIndex(i)) selectionCache.add(i); } addListSelectionListener(this); firePropertyChange(LIST_UPDATED, null, null); } } @SuppressWarnings("unchecked")  CheckBoxJList(); void setSelectedItems(final List<String> selected); @Override void valueChanged(ListSelectionEvent lse); static final String LIST_UPDATED; }