Example usage for javax.swing JList setSelectionModel

List of usage examples for javax.swing JList setSelectionModel

Introduction

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

Prototype

@BeanProperty(description = "The selection model, recording which cells are selected.")
public void setSelectionModel(ListSelectionModel selectionModel) 

Source Link

Document

Sets the selectionModel for the list to a non-null ListSelectionModel implementation.

Usage

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 www .  ja  va  2s  .  c om*/
            }
            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 JList generateListFor(Object... objs) {
    Container c = search(objs, Container.class);
    String[] name_value = search(objs, String[].class);
    ListModel model = search(objs, ListModel.class);
    Object[] dataObjects = search(objs, Object[].class);
    Vector dataVector = search(objs, Vector.class);
    ListSelectionListener selectionListener = search(objs, ListSelectionListener.class);
    ListUI ui = search(objs, ListUI.class);
    ListSelectionModel selectionModel = search(objs, ListSelectionModel.class);
    ListCellRenderer cellRenderer = search(objs, ListCellRenderer.class);
    JList list = model == null
            ? (dataObjects == null ? (dataVector == null ? new JList() : new JList(dataVector))
                    : new JList(dataObjects))
            : new JList(model);
    list.setName(name_value == null ? "" : name_value[0]);
    if (selectionListener != null)
        list.addListSelectionListener(selectionListener);
    if (ui != null)
        list.setUI(ui);/*  w  w  w . j a va  2  s .c  o  m*/
    if (selectionModel != null)
        list.setSelectionModel(selectionModel);
    if (cellRenderer != null)
        list.setCellRenderer(cellRenderer);
    addJContainerListeners(list, objs);
    if (c != null)
        addToContainer(c, list);
    return list;
}

From source file:org.geworkbench.components.lincs.LincsInterface.java

private JScrollPane buildJListPanel(List<String> dataList, JList aJlist) {

    aJlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    aJlist.setSelectionModel(new LincsListSelectionModel());
    aJlist.setModel(new LincsListModel(dataList));
    JScrollPane jScrollPane1 = new javax.swing.JScrollPane(aJlist,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    jScrollPane1.setPreferredSize(new java.awt.Dimension(200, 70));

    return jScrollPane1;

}