set Selection to JList - Java Swing

Java examples for Swing:JList

Description

set Selection to JList

Demo Code


//package com.java2s;

import javax.swing.JList;

public class Main {
    public static void setSelection(JList list, Object element) {
        for (int i = 0; i < list.getModel().getSize(); i++) {
            Object value = (Object) list.getModel().getElementAt(i);
            if (value == element) {
                list.setSelectedIndex(i);
                list.scrollRectToVisible(list.getCellBounds(i, i));
                return;
            }/*from w w w  .ja va  2 s  .c om*/
        }
    }
}

Related Tutorials