Java Swing Tutorial - Java JList .setSelectedIndices (int[] indices)








Syntax

JList.setSelectedIndices(int[] indices) has the following syntax.

public void setSelectedIndices(int[] indices)

Example

In the following code shows how to use JList.setSelectedIndices(int[] indices) method.

//from   w w  w  . ja v a 2s . c om
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;

public class Main {

  public static void main(final String args[]) {
    String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Sizing Samples");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JList jlist1 = new JList(labels);
    jlist1.setVisibleRowCount(4);
    JScrollPane scrollPane1 = new JScrollPane(jlist1);
    frame.add(scrollPane1, BorderLayout.NORTH);
    
    jlist1.setSelectedIndices(new int[]{1,2});
    
    frame.setSize(300, 350);
    frame.setVisible(true);
  }
}