set Selected List item on JList - Java Swing

Java examples for Swing:JList

Description

set Selected List item on JList

Demo Code


//package com.java2s;

import javax.swing.*;

public class Main {
    /**/*from   w  w w.  j ava  2s.c o m*/
     *
     * @param list JList
     * @param selected Object
     */
    public static void setSelectedList(JList list, Object selected) {
        if (list == null) {
            return;
        }
        if (list.getModel().getSize() == 0) {
            return;
        }
        if (selected == null) {
            list.setSelectedIndex(0);
        } else {
            list.setSelectedValue(selected, true);
        }
    }
}

Related Tutorials