Example usage for javax.swing.event ListSelectionEvent getFirstIndex

List of usage examples for javax.swing.event ListSelectionEvent getFirstIndex

Introduction

In this page you can find the example usage for javax.swing.event ListSelectionEvent getFirstIndex.

Prototype

public int getFirstIndex() 

Source Link

Document

Returns the index of the first row whose selection may have changed.

Usage

From source file:Main.java

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H" };
    JFrame frame = new JFrame("Selecting JList");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JList jlist = new JList(labels);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1, BorderLayout.CENTER);

    ListSelectionListener listSelectionListener = new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent listSelectionEvent) {
            System.out.println("First index: " + listSelectionEvent.getFirstIndex());
            System.out.println(", Last index: " + listSelectionEvent.getLastIndex());
            boolean adjust = listSelectionEvent.getValueIsAdjusting();
            System.out.println(", Adjusting? " + adjust);
            if (!adjust) {
                JList list = (JList) listSelectionEvent.getSource();
                int selections[] = list.getSelectedIndices();
                Object selectionValues[] = list.getSelectedValues();
                for (int i = 0, n = selections.length; i < n; i++) {
                    if (i == 0) {
                        System.out.println(" Selections: ");
                    }//w  w  w.  j av a  2s . com
                    System.out.println(selections[i] + "/" + selectionValues[i] + " ");
                }
            }
        }
    };
    jlist.addListSelectionListener(listSelectionListener);

    frame.setSize(350, 200);
    frame.setVisible(true);
}

From source file:MainClass.java

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

    JList jlist = new JList(labels);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1);//from ww w.j  av  a2  s . c  om

    ListSelectionListener listSelectionListener = new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent listSelectionEvent) {
            System.out.println("First index: " + listSelectionEvent.getFirstIndex());
            System.out.println(", Last index: " + listSelectionEvent.getLastIndex());
            boolean adjust = listSelectionEvent.getValueIsAdjusting();
            System.out.println(", Adjusting? " + adjust);
            if (!adjust) {
                JList list = (JList) listSelectionEvent.getSource();
                int selections[] = list.getSelectedIndices();
                Object selectedValues[] = list.getSelectedValues();
                for (int i = 0, n = selections.length; i < n; i++) {
                    if (i == 0) {
                        System.out.println("  Selections: ");
                    }
                    System.out.println(selections[i] + "/" + selectedValues[i] + " ");
                }
            }
        }
    };
    jlist.addListSelectionListener(listSelectionListener);

    frame.setSize(350, 200);
    frame.setVisible(true);
}

From source file:SelectingJListSample.java

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    JFrame frame = new JFrame("Selecting JList");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JList jlist = new JList(labels);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    contentPane.add(scrollPane1, BorderLayout.WEST);

    ListSelectionListener listSelectionListener = new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent listSelectionEvent) {
            System.out.print("First index: " + listSelectionEvent.getFirstIndex());
            System.out.print(", Last index: " + listSelectionEvent.getLastIndex());
            boolean adjust = listSelectionEvent.getValueIsAdjusting();
            System.out.println(", Adjusting? " + adjust);
            if (!adjust) {
                JList list = (JList) listSelectionEvent.getSource();
                int selections[] = list.getSelectedIndices();
                Object selectionValues[] = list.getSelectedValues();
                for (int i = 0, n = selections.length; i < n; i++) {
                    if (i == 0) {
                        System.out.print("  Selections: ");
                    }/* w  ww . j a  v  a 2  s. c o m*/
                    System.out.print(selections[i] + "/" + selectionValues[i] + " ");
                }
                System.out.println();
            }
        }
    };
    jlist.addListSelectionListener(listSelectionListener);

    MouseListener mouseListener = new MouseAdapter() {
        public void mouseClicked(MouseEvent mouseEvent) {
            JList theList = (JList) mouseEvent.getSource();
            if (mouseEvent.getClickCount() == 2) {
                int index = theList.locationToIndex(mouseEvent.getPoint());
                if (index >= 0) {
                    Object o = theList.getModel().getElementAt(index);
                    System.out.println("Double-clicked on: " + o.toString());
                }
            }
        }
    };
    jlist.addMouseListener(mouseListener);

    frame.setSize(350, 200);
    frame.setVisible(true);
}

From source file:SelectionMonitor.java

public void valueChanged(ListSelectionEvent e) {
    if ((!e.getValueIsAdjusting()) || (e.getFirstIndex() == -1))
        return;/*from  ww w  . ja  va 2  s. co  m*/

    for (int i = e.getFirstIndex(); i <= e.getLastIndex(); i++) {
        System.out.println(((JList) e.getSource()).isSelectedIndex(i));
    }
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String items[] = { "A", "B", "C", "D" };
    JList list = new JList(items);
    ListSelectionModel selModel = list.getSelectionModel();

    selModel.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                System.out.println("selection changed: " + e.getFirstIndex());
            }//from  w  w w  .  jav a2 s .co  m
        }
    });
    getContentPane().add(list);
    pack();
    setVisible(true);
}

From source file:Main.java

public void valueChanged(ListSelectionEvent e) {
    if (e.getSource() == table.getSelectionModel() && table.getRowSelectionAllowed()) {
        int first = e.getFirstIndex();
        int last = e.getLastIndex();
    } else if (e.getSource() == table.getColumnModel().getSelectionModel()
            && table.getColumnSelectionAllowed()) {
        int first = e.getFirstIndex();
        int last = e.getLastIndex();
    }/*from  w  w w  . jav  a 2 s .c o m*/
    if (e.getValueIsAdjusting()) {
        System.out.println("The mouse button has not yet been released");
    }
}

From source file:net.sf.maltcms.chromaui.charts.events.PeakListSelectionListener.java

@Override
public void valueChanged(ListSelectionEvent e) {
    int index = e.getFirstIndex();
    XYAnnotation xya = (XYAnnotation) this.xyalm.get(index);
    fireEvent(new AEvent<>(xya, this, "XYANNOTATION_SELECT"));
}

From source file:SharedListSelectionHandler.java

public void valueChanged(ListSelectionEvent e) {
    ListSelectionModel lsm = (ListSelectionModel) e.getSource();

    int firstIndex = e.getFirstIndex();
    int lastIndex = e.getLastIndex();
    boolean isAdjusting = e.getValueIsAdjusting();
    System.out.println("Event for indexes " + firstIndex + " - " + lastIndex + "; isAdjusting is " + isAdjusting
            + "; selected indexes:");

    if (lsm.isSelectionEmpty()) {
        System.out.println(" <none>");
    } else {/*from www  .  j a  v  a 2  s .c om*/
        // Find out which indexes are selected.
        int minIndex = lsm.getMinSelectionIndex();
        int maxIndex = lsm.getMaxSelectionIndex();
        for (int i = minIndex; i <= maxIndex; i++) {
            if (lsm.isSelectedIndex(i)) {
                System.out.println(" " + i);
            }
        }
    }
}

From source file:Main.java

Main() {
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    ArrayList data = new ArrayList();
    data.add("Hi");
    data.add("Hello");
    data.add("Goodbye");
    list = new JList(data.toArray());
    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent evt) {
            if (evt.getValueIsAdjusting())
                return;
            System.out.println("Selected from " + evt.getFirstIndex() + " to " + evt.getLastIndex());
        }//  w  w w.  j a  v  a2  s.  c o m
    });
    cp.add(new JScrollPane(list), BorderLayout.CENTER);
}

From source file:MainClass.java

MainClass() {
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    ArrayList data = new ArrayList();
    data.add("Hi");
    data.add("Hello");
    data.add("Goodbye");
    list = new JList(data.toArray());
    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent evt) {
            if (evt.getValueIsAdjusting())
                return;
            System.out.println("Selected from " + evt.getFirstIndex() + " to " + evt.getLastIndex());
        }/* w  w w  . j ava 2  s. c  o m*/
    });
    cp.add(new JScrollPane(list), BorderLayout.CENTER);
}