Example usage for javax.swing JList JList

List of usage examples for javax.swing JList JList

Introduction

In this page you can find the example usage for javax.swing JList JList.

Prototype

public JList(final Vector<? extends E> listData) 

Source Link

Document

Constructs a JList that displays the elements in the specified Vector.

Usage

From source file:Main.java

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

    list.setSelectionMode(DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultListModel model = new DefaultListModel();
    JList list = new JList(model);

    int pos = 0;/* w  w  w .j a  v  a  2s  .c om*/
    model.add(pos, "a");
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultListModel model = new DefaultListModel();
    JList list = new JList(model);

    // Replace the 2nd item
    int pos = 1;//from   w w w  .j  a va2  s .c  o  m
    model.set(pos, "b");
}

From source file:Main.java

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

    // Get the current selection model mode
    int mode = list.getSelectionMode(); // MULTIPLE_INTERVAL_SELECTION

    // Only one item can be selected
    list.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultListModel model = new DefaultListModel();
    JList list = new JList(model);

    // Initialize the list with items
    String[] items = { "A", "B", "C", "D" };
    for (int i = 0; i < items.length; i++) {
        model.add(i, items[i]);/*from  ww w .  j av a2  s  .c  o  m*/

    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultListModel model = new DefaultListModel();
    JList list = new JList(model);

    // Remove the first item
    int pos = 0;/*w  w w  .  j av  a 2 s.  co  m*/
    model.remove(pos);

    // Remove the last item
    pos = model.getSize() - 1;
    if (pos >= 0) {
        model.remove(pos);
    }

    // Remove all items
    model.clear();
}

From source file:Main.java

public static void main(String[] args) {
    String[] items = { "1|9|2", "1|9|1", "1|4|7" };
    JList list = new JList(items);
    JPanel panel = new JPanel();
    panel.add(new JScrollPane(list));
    JOptionPane.showMessageDialog(null, panel);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JList list = new JList(new String[] { "one", "two", "three", "four" });
    list.setSelectionModel(new DefaultListSelectionModel() {
        public void setSelectionInterval(int index0, int index1) {
            if (index0 == index1) {
                if (isSelectedIndex(index0)) {
                    removeSelectionInterval(index0, index0);
                    return;
                }/*from  w  w w . j a  v  a2  s .c  o  m*/
            }
            super.setSelectionInterval(index0, index1);
        }

        @Override
        public void addSelectionInterval(int index0, int index1) {
            if (index0 == index1) {
                if (isSelectedIndex(index0)) {
                    removeSelectionInterval(index0, index0);
                    return;
                }
                super.addSelectionInterval(index0, index1);
            }
        }

    });
    f.getContentPane().add(list);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

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

    list.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            JList list = (JList) evt.getSource();
            if (evt.getClickCount() == 2) { // Double-click
                int index = list.locationToIndex(evt.getPoint());
            } else if (evt.getClickCount() == 3) { // Triple-click
                int index = list.locationToIndex(evt.getPoint());
            }/*  w w w.  jav a  2s.com*/
        }
    });
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create a list that allows adds and removes
    DefaultListModel model = new DefaultListModel();
    JList list = new JList(model);

    int pos = list.getModel().getSize();
    model.add(pos, "E");
}