Java DefaultListSelectionModel .getLeadSelectionIndex ()

Syntax

DefaultListSelectionModel.getLeadSelectionIndex() has the following syntax.

public int getLeadSelectionIndex()

Example

In the following code shows how to use DefaultListSelectionModel.getLeadSelectionIndex() method.


//from   w  ww  .  ja  va  2  s . com
import java.awt.BorderLayout;

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

public class Main extends JPanel {

  String label[] = { "Zero", "One", "Two" };

  JList list;

  public Main() {
    setLayout(new BorderLayout());

    list = new JList(label);
    JScrollPane pane = new JScrollPane(list);

    DefaultListSelectionModel m = new DefaultListSelectionModel();
    
    list.setSelectionModel(m);

    System.out.println(m.getLeadSelectionIndex());
    
    add(pane, BorderLayout.NORTH);
  }

  public static void main(String s[]) {
    JFrame frame = new JFrame("List Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new Main());
    frame.pack();
    frame.setVisible(true);
  }

}