Java Utililty Methods JList Select

List of utility methods to do JList Select

Description

The list of methods to do JList Select are organized into topic(s).

Method

voidremoveSelectionFromList(JList fileList)
Removes the selected entry from the given list.
if (fileList.getSelectedIndex() > -1) {
    ((DefaultListModel) fileList.getModel()).remove(fileList.getSelectedIndex());
voidremoveTypeSelectionListener(JList list)
Every list component during construction registers a special handler that lets the user type letters and select elements in the list.
KeyListener[] kls = list.getListeners(KeyListener.class);
for (KeyListener kl : kls) {
    String name = kl.getClass().getName();
    if (name.endsWith("BasicListUI$Handler"))
        list.removeKeyListener(kl);
voidselectByTyping(javax.swing.JList list, javax.swing.JTextField textfield)
This method selects the first entry in the JList list that start with the text that is entered in the filter-textfield textfield .
String text = textfield.getText().toLowerCase();
ListModel lm = list.getModel();
for (int cnt = 0; cnt < lm.getSize(); cnt++) {
    String val = lm.getElementAt(cnt).toString();
    if (val.toLowerCase().startsWith(text)) {
        list.setSelectedIndex(cnt);
        list.ensureIndexIsVisible(cnt);
        return;
...
voidselectRow(int row, ListSelectionModel model)
select Row
if (model == null) {
    return;
model.setSelectionInterval(row, row);
voidselectStringInList(String string, JList list)
Select an item in a JList given an items string value
for (int i = 0; i < list.getModel().getSize(); i++) {
    if (String.valueOf(list.getModel().getElementAt(i)).equals(string)) {
        list.setSelectedIndex(i);
        return;
voidsetJListSelection(List selection, JList list)
Sets the selected items on a JList , which is a tedious task to do by hand.
List modelItems = new ArrayList();
ListModel listModel = list.getModel();
for (int i = 0; i < listModel.getSize(); i++) {
    modelItems.add(listModel.getElementAt(i));
List indices = new ArrayList();
Iterator items = selection.iterator();
while (items.hasNext()) {
...
voidsetLeadAnchorWithoutSelection(ListSelectionModel model, int lead, int anchor)
Set the lead and anchor without affecting selection.
if (anchor == -1) {
    anchor = lead;
if (lead == -1) {
    model.setAnchorSelectionIndex(-1);
    model.setLeadSelectionIndex(-1);
} else {
    if (model.isSelectedIndex(lead)) {
...
voidsetSelectedItems(JList list, List selected)
_more_
List items = getItems(list);
int cnt = 0;
int[] indices = new int[items.size()];
for (int i = 0; i < selected.size(); i++) {
    int idx = items.indexOf(selected.get(i));
    if (idx < 0) {
        continue;
    indices[cnt++] = idx;
if (cnt > 0) {
    int[] realIndices = new int[cnt];
    for (int i = 0; i < cnt; i++) {
        realIndices[i] = indices[i];
    list.setSelectedIndices(realIndices);
voidsetSelectedList(JList list, Object selected)
set Selected List
if (list == null) {
    return;
if (list.getModel().getSize() == 0) {
    return;
if (selected == null) {
    list.setSelectedIndex(0);
...
voidsetSelectedListIndices(final JList list, final List indices)
set Selected List Indices
final int[] selIndex = new int[indices.size()];
for (int i = 0; i < indices.size(); ++i) {
    selIndex[i] = indices.get(i);
list.setSelectedIndices(selIndex);