Java Swing How to - Determine if the third item is selected








Question

We would like to know how to determine if the third item is selected.

Answer

/* w w  w.  ja va  2  s  .  c o m*/
import javax.swing.JList;

public class Main {
  public static void main(String[] argv) throws Exception {
    String[] items = { "A", "B", "C", "D" };
    JList list = new JList(items);

    int index = 2;
    boolean isSel = list.isSelectedIndex(index);
  }
}