List<Product> getSelectedProducts() { List<Product> selectedProducts = new ArrayList<>(); for (int i = 0; i < productsList.getModel().getSize(); i++) { if (productsList.getCheckBoxListSelectionModel().isSelectedIndex(i)) { selectedProducts.add((Product) productsList.getModel().getElementAt(i)); } } return selectedProducts; }
List<Product> getSelectedProducts() { List<Product> selectedProducts = new ArrayList<>(); for (int i = 0; i < productsList.getModel().getSize(); i++) { if (productsList.getCheckBoxListSelectionModel().isSelectedIndex(i)) { selectedProducts.add((Product) productsList.getModel().getElementAt(i)); } } return selectedProducts; }
@Override public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { final CheckBoxListSelectionModel selectionModel = variableList.getCheckBoxListSelectionModel(); for (int i = e.getFirstIndex(); i <= e.getLastIndex(); i++) { model.setSelectedVariableAt(i, selectionModel.isSelectedIndex(i)); } } } }
@Override public void run() { final String text = maskNameSearchField.getText(); final DefaultListModel<Object> updatedListModel = new DefaultListModel<>(); List<Boolean> selected = new ArrayList<>(); int[] newIndexesInMaskNameList = new int[maskNameListModel.getSize()]; int counter = 0; for (int i = 0; i < maskNameListModel.getSize(); i++) { if (maskNameListModel.get(i).toLowerCase().contains(text.toLowerCase())) { updatedListModel.addElement(maskNameListModel.get(i)); if (indexesInMaskNameList[i] >= 0) { selected.add(maskNameList.getCheckBoxListSelectionModel().isSelectedIndex(indexesInMaskNameList[i])); } else { selected.add(false); } newIndexesInMaskNameList[i] = counter++; } else { newIndexesInMaskNameList[i] = -1; } } indexesInMaskNameList = newIndexesInMaskNameList; maskNameList.setModel(updatedListModel); for (int i = 0; i < selected.size(); i++) { if (selected.get(i)) { maskNameList.getCheckBoxListSelectionModel().addSelectionInterval(i, i); } } } });
private void selectAllIf() { if (getAllEntryIndex() < 0) { return; } for (int i = getModel().getSize() - 1; i >= 0; i--) { if (i != getAllEntryIndex() && !isSelectedIndex(i)) { return; } } super.addSelectionInterval(getAllEntryIndex(), getAllEntryIndex()); }
protected void toggleSelections() { int[] indices = _list.getSelectedIndices(); CheckBoxListSelectionModel selectionModel = _list.getCheckBoxListSelectionModel(); selectionModel.removeListSelectionListener(this); selectionModel.setValueIsAdjusting(true); try { if (indices.length > 0) { boolean selected = selectionModel.isSelectedIndex(indices[0]); for (int index : indices) { if (!_list.isCheckBoxEnabled(index)) { continue; } if (selected && selectionModel.isSelectedIndex(index)) { selectionModel.removeSelectionInterval(index, index); } else if (!selected && !selectionModel.isSelectedIndex(index)) { selectionModel.addSelectionInterval(index, index); } } } } finally { selectionModel.setValueIsAdjusting(false); selectionModel.addListSelectionListener(this); _list.repaint(); } }
continue; if (listSelectionModel.isSelectedIndex(i)) { temp[n] = model.getElementAt(i); n++;
/** * Returns an array of all of the selected indices in increasing order. * * @return all of the selected indices, in increasing order * @see #removeSelectionInterval * @see #addListSelectionListener */ public int[] getCheckBoxListSelectedIndices() { CheckBoxListSelectionModel listSelectionModel = getCheckBoxListSelectionModel(); int iMin = listSelectionModel.getMinSelectionIndex(); int iMax = listSelectionModel.getMaxSelectionIndex(); if ((iMin < 0) || (iMax < 0)) { return new int[0]; } int[] temp = new int[1 + (iMax - iMin)]; int n = 0; for (int i = iMin; i <= iMax; i++) { if (listSelectionModel.isAllEntryConsidered() && i == listSelectionModel.getAllEntryIndex()) { continue; } if (listSelectionModel.isSelectedIndex(i)) { temp[n] = i; n++; } } int[] indices = new int[n]; System.arraycopy(temp, 0, indices, 0, n); return indices; }
protected void toggleSelection(int index) { if (!_list.isEnabled() || !_list.isCheckBoxEnabled(index)) { return; } CheckBoxListSelectionModel selectionModel = _list.getCheckBoxListSelectionModel(); boolean selected = selectionModel.isSelectedIndex(index); selectionModel.removeListSelectionListener(this); try { if (selected) selectionModel.removeSelectionInterval(index, index); else selectionModel.addSelectionInterval(index, index); } finally { selectionModel.addListSelectionListener(this); _list.repaint(); } }
boolean old = isSelectedIndex(index); boolean adjusting = getValueIsAdjusting(); super.setValueIsAdjusting(true);
_checkBox.setSelected(selectionModel.isSelectedIndex(index));