/** * Deselects all objects in this list. */ public void selectNone() { if (getModel().getSize() > 0) { getCheckBoxListSelectionModel().removeIndexInterval(0, getModel().getSize() - 1); } }
private void selectAndEnableCheckBoxes() { final int numEntries = maskNameList.getModel().getSize(); final int numSelected = maskNameList.getCheckBoxListSelectedIndices().length; selectNoneCheckBox.setEnabled(numSelected > 0); selectAllCheckBox.setEnabled(numSelected < numEntries); selectNoneCheckBox.setSelected(numSelected == 0); selectAllCheckBox.setSelected(numSelected == numEntries); }
public Collection<T> getSelectedTracks() { if (isCancelled) return null; Object[] selectedObjects = trackList.getCheckBoxListSelectedValues(); if (selectedObjects == null || selectedObjects.length == 0) return null; List<T> selectedTracks = new ArrayList<T>(selectedObjects.length); for(Object object: selectedObjects){ selectedTracks.add(((TrackWrapper<T>) object).getTrack()); } return selectedTracks; }
/** * Deselects the specified object from the list. * * @param anObject the object to select * @param shouldScroll true if the list should scroll to display the selected object, if one exists; otherwise * false */ public void removeCheckBoxListSelectedValue(Object anObject, boolean shouldScroll) { if (anObject != null) { int i, c; ListModel model = getModel(); for (i = 0, c = model.getSize(); i < c; i++) if (anObject.equals(model.getElementAt(i))) { removeCheckBoxListSelectedIndex(i); if (shouldScroll) ensureIndexIsVisible(i); repaint(); /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/ return; } } }
/** * Initialize the CheckBoxList. */ protected void init() { _checkBoxListSelectionModel = createCheckBoxListSelectionModel(getModel()); setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); _listCellRenderer = createCellRenderer(); _handler = createHandler(); _checkBoxListSelectionModel.addListSelectionListener(_handler); JideSwingUtilities.insertMouseListener(this, _handler, 0); JideSwingUtilities.insertMouseMotionListener(this, _handler, 0); addKeyListener(_handler); addPropertyChangeListener("model", _handler); ListModel model = getModel(); if (model != null) { model.addListDataListener(_handler); } }
@Override public void setModel(ListModel model) { super.setModel(model); if (getCheckBoxListSelectionModel() != null) { getCheckBoxListSelectionModel().clearSelection(); } }
private void createPane() { final TableLayout tableLayout = new TableLayout(1); tableLayout.setTableAnchor(TableLayout.Anchor.NORTHWEST); tableLayout.setTableFill(TableLayout.Fill.BOTH); tableLayout.setTablePadding(4, 4); tableLayout.setTableWeightY(1.0); tableLayout.setTableWeightX(1.0); setLayout(tableLayout); variableList = new CheckBoxList(model); variableList.setCellRenderer(new DefaultListCellRenderer() { @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (value instanceof Variable) { Variable variable = (Variable) value; label.setText(variable.getName()); } return label; } }); variableList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); checkBoxSelectionListener = new CheckBoxSelectionListener(variableList); variableList.getCheckBoxListSelectionModel().addListSelectionListener(checkBoxSelectionListener); add(new JScrollPane(variableList)); }
selectNone.setEnabled(false); productsList = new CheckBoxList(listModel); productsList.setCellRenderer(new ProductListCellRenderer()); productsList.getCheckBoxListSelectionModel().addListSelectionListener(new ListSelectionListener() {
/** * Selects the specified object from the list and clear all other selections. * * @param anObject the object to select * @param shouldScroll true if the list should scroll to display the selected object, if one exists; otherwise * false */ public void setCheckBoxListSelectedValue(Object anObject, boolean shouldScroll) { if (anObject == null) setSelectedIndex(-1); else { int i, c; ListModel model = getModel(); for (i = 0, c = model.getSize(); i < c; i++) if (anObject.equals(model.getElementAt(i))) { setCheckBoxListSelectedIndex(i); if (shouldScroll) ensureIndexIsVisible(i); repaint(); /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/ return; } setCheckBoxListSelectedIndex(-1); } repaint(); /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/ }
void updateEnablement() { boolean hasMasks = (product != null && product.getMaskGroup().getNodeCount() > 0); boolean canSelectMasks = hasMasks && useRoiCheckBox.isSelected(); useRoiCheckBox.setEnabled(hasMasks); maskNameSearchField.setEnabled(canSelectMasks); maskNameList.setEnabled(canSelectMasks); selectAllCheckBox.setEnabled(canSelectMasks && maskNameList.getCheckBoxListSelectedIndices().length < maskNameList.getModel().getSize()); selectNoneCheckBox.setEnabled(canSelectMasks && maskNameList.getCheckBoxListSelectedIndices().length > 0); refreshButton.setEnabled(raster != null); }
private void updatePane() { variableList.getCheckBoxListSelectionModel().removeListSelectionListener(checkBoxSelectionListener); variableList.setModel(model); variableList.setCheckBoxListSelectedIndices(getSelectedIndices(model)); final CheckBoxListSelectionModel selectionModel = variableList.getCheckBoxListSelectionModel(); selectionModel.addListSelectionListener(checkBoxSelectionListener); }
@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); } } } });
ListModel model = getModel(); int i, c; if (anObject != null) { for (i = 0, c = model.getSize(); i < c; i++) if (anObject.equals(model.getElementAt(i))) { addCheckBoxListSelectedIndex(i); if (shouldScroll) ensureIndexIsVisible(i); repaint(); /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/ return; for (i = 0, c = model.getSize(); i < c; i++) { if (model.getElementAt(i) == null) { addCheckBoxListSelectedIndex(i); if (shouldScroll) ensureIndexIsVisible(i); repaint(); /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/ return;
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(); } }
maskNameList = new CheckBoxList(new DefaultListModel()); maskNameList.getCheckBoxListSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); maskNameList.getCheckBoxListSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) {
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(); } }
/** * Deselects the specified objects from the list and keep all previous selections. * * @param objects the objects to be selected */ public void removeCheckBoxListSelectedValues(Object[] objects) { if (objects != null) { Map<Object, String> map = new HashMap<Object, String>(); for (Object o : objects) { map.put(o, ""); } int i, c; ListModel model = getModel(); boolean changed = false; for (i = 0, c = model.getSize(); i < c; i++) if (map.get(model.getElementAt(i)) != null) { removeCheckBoxListSelectedIndex(i); changed = true; } if (changed) { repaint(); } map.clear(); } }
/** * Selects the specified objects from the list and keep all previous selections. * * @param objects the objects to be selected */ public void addCheckBoxListSelectedValues(Object[] objects) { if (objects != null) { Map<Object, String> map = new HashMap<Object, String>(); for (Object o : objects) { map.put(o, ""); } int i, c; ListModel model = getModel(); boolean changed = false; for (i = 0, c = model.getSize(); i < c; i++) if (map.get(model.getElementAt(i)) != null) { addCheckBoxListSelectedIndex(i); changed = true; } if (changed) { repaint(); } map.clear(); } }
contentPanel = new JPanel(); trackPanel = new JScrollPane(); trackList = new CheckBoxList(); initTrackList(); buttonBar = new JPanel(); trackList.setClickInCheckBoxOnly(false); trackList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); trackPanel.setViewportView(trackList);
private void setSelectedObjects(Map<Object, String> selected) { List<Integer> indices = new ArrayList<Integer>(); for (int i = 0; i < getModel().getSize(); i++) { Object elementAt = getModel().getElementAt(i); if (selected.get(elementAt) != null) { indices.add(i); } } int[] selectedIndices = new int[indices.size()]; for (int i = 0; i < indices.size(); i++) { Integer row = indices.get(i); selectedIndices[i] = row; } setCheckBoxListSelectedIndices(selectedIndices); }