Example usage for javax.swing.event ListSelectionEvent getSource

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

Introduction

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

Prototype

public Object getSource() 

Source Link

Document

The object on which the Event initially occurred.

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: ");
                    }//from   w  ww  . ja v a 2  s. c o m
                    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.  ja  va 2s. c o m*/

    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 w w . j  av  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: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  .  j a  v  a 2  s .  c o  m*/
    if (e.getValueIsAdjusting()) {
        System.out.println("The mouse button has not yet been released");
    }
}

From source file:Main.java

@Override
public void valueChanged(final ListSelectionEvent e) {
    JList list = (JList) e.getSource();
    list.ensureIndexIsVisible(list.getSelectedIndex());
}

From source file:ListTest.java

public void valueChanged(ListSelectionEvent evt) {
    JList source = (JList) evt.getSource();
    Object[] values = source.getSelectedValues();

    String text = "";
    for (int i = 0; i < values.length; i++) {
        String word = (String) values[i];
        text += word + " ";
    }//from ww  w .j a  v  a  2  s  . c  om
    label.setText("The " + text + "fox jumps over the lazy dog.");
}

From source file:LongListTest.java

public void valueChanged(ListSelectionEvent evt) {
    JList source = (JList) evt.getSource();
    String word = (String) source.getSelectedValue();
    label.setText(word);/*from  www. j  a v a 2s  .c o  m*/
}

From source file:Main.java

public void valueChanged(ListSelectionEvent evt) {
    if (!evt.getValueIsAdjusting()) {
        JList list = (JList) evt.getSource();
        Object[] selected = list.getSelectedValues();

        for (int i = 0; i < selected.length; i++) {
            Object sel = selected[i];
        }//from w  ww  .  ja v a  2s.c o  m
    }
}

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 ww  w  . java2  s . c  o m
        // 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

public void valueChanged(ListSelectionEvent evt) {
    JList source = (JList) evt.getSource();
    Font font = (Font) source.getSelectedValue();
    label.setFont(font);//  w w  w .  jav  a 2  s .  c o  m
}