Java Code Examples for javax.swing.JList#addSelectionInterval()

The following examples show how to use javax.swing.JList#addSelectionInterval() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void reloadField (JComponent component, IssueField field) {
    String newValue;
    newValue = task.getFieldValue(field);
    boolean fieldDirty = unsavedFields.contains(field.getKey());
    if (!fieldDirty) {
        if (component instanceof JComboBox) {
            throw new UnsupportedOperationException();
        } else if (component instanceof JTextComponent) {
            ((JTextComponent) component).setText(newValue);
        } else if (component instanceof JList) {
            JList list = (JList) component;
            list.clearSelection();
            ListModel model = list.getModel();
            for (String value : task.getFieldValues(field)) {
                for (int i = 0; i < model.getSize(); i++) {
                    if (value.equals(model.getElementAt(i))) {
                        list.addSelectionInterval(i, i);
                    }
                }
            }
        } else if (component instanceof JCheckBox) {
            ((JCheckBox) component).setSelected("1".equals(newValue));
        }
    }
}
 
Example 2
Source File: ExtendedJListTransferHandler.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean importData(TransferSupport info) {
	TransferHandler.DropLocation dropLocation = info.getDropLocation();
	if (!canImport(info) || !(dropLocation instanceof JList.DropLocation)) {
		return false;
	}

	JList.DropLocation dl = (JList.DropLocation) dropLocation;
	JList target = (JList) info.getComponent();
	DefaultListModel listModel = (DefaultListModel) target.getModel();
	int max = listModel.getSize();
	int index = dl.getIndex();
	index = index < 0 ? max : index;
	// make sure to append at the end if index > size
	index = Math.min(index, max);

	addIndex = index;

	try {
		Object[] values = (Object[]) info.getTransferable().getTransferData(localObjectFlavor);
		for (Object value : values) {
			int idx = index++;
			listModel.add(idx, value);
			target.addSelectionInterval(idx, idx);
		}
		addCount = values.length;
		return true;
	} catch (UnsupportedFlavorException | IOException e) {
		// should never happen, log anyway to be safe
		LogService.getRoot().log(Level.WARNING, "com.rapidminer.gui.tools.dnd.ExtendedJListTransferHandler.unexpected_error", e);
	}

	return false;
}
 
Example 3
Source File: ShowToolSettingsPanel.java    From jeveassets with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean importData(TransferSupport info) {
	TransferHandler.DropLocation tdl = info.getDropLocation();
	if (!canImport(info) || !(tdl instanceof JList.DropLocation)) {
		return false;
	}

	JList.DropLocation dl = (JList.DropLocation) tdl;
	JList<?> target = (JList) info.getComponent();
	DefaultListModel<Object> listModel = (DefaultListModel) target.getModel();
	int max = listModel.getSize();
	int index = dl.getIndex();
	index = index < 0 ? max : index; // If it is out of range, it is appended to the end
	index = Math.min(index, max);

	addIndex = index;

	try {
		Object[] values = (Object[]) info.getTransferable().getTransferData(localObjectFlavor);
		for (Object value : values) {
			int idx = index++;
			listModel.add(idx, value);
			target.addSelectionInterval(idx, idx);
		}
		addCount = values.length;
		return true;
	} catch (UnsupportedFlavorException | IOException ex) {
		LOG.error(ex.getMessage(), ex);
		return false;
	}
}
 
Example 4
Source File: NamedStoredProcedureQueryPanel.java    From jeddict with Apache License 2.0 5 votes vote down vote up
private void setResultClassSelectedValues(JList list, Object... values) {
    list.clearSelection();
    for (Object value : values) {
        int index = getElementIndexInList(list.getModel(), value);
        if (index >= 0) {
            list.addSelectionInterval(index, index);
        } else if (value instanceof String) {  //if external lib class not exists then add
            addAndSelectItemInList(list, value);
        }
    }
    list.ensureIndexIsVisible(list.getSelectedIndex());
}
 
Example 5
Source File: NamedStoredProcedureQueryPanel.java    From jeddict with Apache License 2.0 4 votes vote down vote up
private void addAndSelectItemInList(JList list, Object value) {
    ((DefaultListModel) list.getModel()).addElement(value);
    int index = getElementIndexInList(list.getModel(), value);
    list.addSelectionInterval(index, index);
}
 
Example 6
Source File: MainActivity.java    From Raccoon with Apache License 2.0 4 votes vote down vote up
private void doImport() {
	try {
		String data = (String) Toolkit.getDefaultToolkit().getSystemClipboard()
				.getData(DataFlavor.stringFlavor);
		String prefix = "market://details?id="; //$NON-NLS-1$
		StringTokenizer st = new StringTokenizer(data);
		Vector<String> lst = new Vector<String>();
		int count = 0;
		while (st.hasMoreElements()) {
			// Lets first check the entire clipboard if its content is well formed.
			String url = st.nextToken();
			if (url.startsWith(prefix) && url.length() > prefix.length()) {
				// Let's keep it simple.
				String id = url.substring(prefix.length(), url.length());
				if (!lst.contains(id) && !archive.fileUnder(id, 0).getParentFile().exists()) {
					lst.add(id);
					count++;
				}
			}
		}

		// We got at least one new app id. Ask the user to confirm the list, then
		// create files.
		if (count > 0) {
			JList<String> all = new JList<String>(lst);
			all.addSelectionInterval(0, lst.size() - 1);
			if (JOptionPane
					.showConfirmDialog(
							this,
							new JScrollPane(all),
							Messages.getString("MainActivity.42"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null) == JOptionPane.OK_OPTION) {//$NON-NLS-1$
				int[] sel = all.getSelectedIndices();
				for (int idx : sel) {
					archive.fileUnder(all.getModel().getElementAt(idx), 0).getParentFile().mkdirs();
				}
				searchView.doMessage(Messages.getString("MainActivity.39")); //$NON-NLS-1$
			}
		}
		else {
			// Tell the user that no new items were found.
			searchView.doMessage(Messages.getString("MainActivity.43")); //$NON-NLS-1$
		}
	}
	catch (Exception e) {
		searchView.doMessage(Messages.getString("MainActivity.40")); //$NON-NLS-1$
		// e.printStackTrace();
	}
}