Example usage for javax.swing JList getSelectionModel

List of usage examples for javax.swing JList getSelectionModel

Introduction

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

Prototype

public ListSelectionModel getSelectionModel() 

Source Link

Document

Returns the current selection model.

Usage

From source file:JListSelectionModeAnchor.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Modifying Model");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JList jlist = new JList(new String[] { "A", "B", "C" });

    jlist.getSelectionModel().setAnchorSelectionIndex(0);
    jlist.getSelectionModel().setLeadSelectionIndex(2);

    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1, BorderLayout.CENTER);

    frame.setSize(640, 300);//w  w w. j a v a2s . c  om
    frame.setVisible(true);
}

From source file:Main.java

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

    JList jlist1 = new JList();
    jlist1.setListData(labels);//from  w  w w  .j  a va 2s .c o  m

    jlist1.setVisibleRowCount(4);
    JScrollPane scrollPane1 = new JScrollPane(jlist1);
    frame.add(scrollPane1, BorderLayout.NORTH);

    ListSelectionModel model = jlist1.getSelectionModel();

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

From source file:Main.java

public static void main(String[] args) {
    final DefaultListModel<String> model = new DefaultListModel<>();
    final JList<String> list = new JList<>(model);
    JFrame f = new JFrame();

    model.addElement("A");
    model.addElement("B");
    model.addElement("C");
    model.addElement("D");
    model.addElement("E");

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

    JPanel leftPanel = new JPanel();
    JPanel rightPanel = new JPanel();

    leftPanel.setLayout(new BorderLayout());
    rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));

    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

    list.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                int index = list.locationToIndex(e.getPoint());
                Object item = model.getElementAt(index);
                String text = JOptionPane.showInputDialog("Rename item", item);
                String newitem = "";
                if (text != null)
                    newitem = text.trim();
                else
                    return;

                if (!newitem.isEmpty()) {
                    model.remove(index);
                    model.add(index, newitem);
                    ListSelectionModel selmodel = list.getSelectionModel();
                    selmodel.setLeadSelectionIndex(index);
                }/*  w ww .  j a  v a 2s. co  m*/
            }
        }
    });
    leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    leftPanel.add(new JScrollPane(list));

    JButton removeall = new JButton("Remove All");
    JButton add = new JButton("Add");
    JButton rename = new JButton("Rename");
    JButton delete = new JButton("Delete");

    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = JOptionPane.showInputDialog("Add a new item");
            String item = null;

            if (text != null)
                item = text.trim();
            else
                return;

            if (!item.isEmpty())
                model.addElement(item);
        }
    });

    delete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            ListSelectionModel selmodel = list.getSelectionModel();
            int index = selmodel.getMinSelectionIndex();
            if (index >= 0)
                model.remove(index);
        }

    });

    rename.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ListSelectionModel selmodel = list.getSelectionModel();
            int index = selmodel.getMinSelectionIndex();
            if (index == -1)
                return;
            Object item = model.getElementAt(index);
            String text = JOptionPane.showInputDialog("Rename item", item);
            String newitem = null;

            if (text != null) {
                newitem = text.trim();
            } else
                return;

            if (!newitem.isEmpty()) {
                model.remove(index);
                model.add(index, newitem);
            }
        }
    });

    removeall.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.clear();
        }
    });

    rightPanel.add(add);
    rightPanel.add(rename);
    rightPanel.add(delete);
    rightPanel.add(removeall);

    rightPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));

    panel.add(leftPanel);
    panel.add(rightPanel);

    f.add(panel);

    f.setSize(350, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

}

From source file:List.java

public static void main(String[] args) {
    final DefaultListModel model = new DefaultListModel();
    final JList list = new JList(model);
    JFrame f = new JFrame();
    f.setTitle("JList models");

    model.addElement("A");
    model.addElement("B");
    model.addElement("C");
    model.addElement("D");
    model.addElement("E");

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

    JPanel leftPanel = new JPanel();
    JPanel rightPanel = new JPanel();

    leftPanel.setLayout(new BorderLayout());
    rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));

    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

    list.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                int index = list.locationToIndex(e.getPoint());
                Object item = model.getElementAt(index);
                String text = JOptionPane.showInputDialog("Rename item", item);
                String newitem = "";
                if (text != null)
                    newitem = text.trim();
                else
                    return;

                if (!newitem.isEmpty()) {
                    model.remove(index);
                    model.add(index, newitem);
                    ListSelectionModel selmodel = list.getSelectionModel();
                    selmodel.setLeadSelectionIndex(index);
                }//from  ww  w .  ja v a2  s .  co m
            }
        }
    });
    leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    leftPanel.add(new JScrollPane(list));

    JButton removeall = new JButton("Remove All");
    JButton add = new JButton("Add");
    JButton rename = new JButton("Rename");
    JButton delete = new JButton("Delete");

    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = JOptionPane.showInputDialog("Add a new item");
            String item = null;

            if (text != null)
                item = text.trim();
            else
                return;

            if (!item.isEmpty())
                model.addElement(item);
        }
    });

    delete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            ListSelectionModel selmodel = list.getSelectionModel();
            int index = selmodel.getMinSelectionIndex();
            if (index >= 0)
                model.remove(index);
        }

    });

    rename.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ListSelectionModel selmodel = list.getSelectionModel();
            int index = selmodel.getMinSelectionIndex();
            if (index == -1)
                return;
            Object item = model.getElementAt(index);
            String text = JOptionPane.showInputDialog("Rename item", item);
            String newitem = null;

            if (text != null) {
                newitem = text.trim();
            } else
                return;

            if (!newitem.isEmpty()) {
                model.remove(index);
                model.add(index, newitem);
            }
        }
    });

    removeall.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.clear();
        }
    });

    rightPanel.add(add);
    rightPanel.add(rename);
    rightPanel.add(delete);
    rightPanel.add(removeall);

    rightPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));

    panel.add(leftPanel);
    panel.add(rightPanel);

    f.add(panel);

    f.setSize(350, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

}

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   ww w  . j  ava2  s .  c om
        }
    });
    getContentPane().add(list);
    pack();
    setVisible(true);
}

From source file:ListIt.java

public ListIt() {
    JFrame f = new JFrame();
    final PartsListModel pcm = new PartsListModel();
    ListCellRenderer lcr = new MyLabelRenderer();
    JList jl = new JList(pcm);
    jl.setCellRenderer(lcr);/*from  ww  w . java2  s. c  o m*/
    ListSelectionModel lsm = jl.getSelectionModel();
    lsm.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jl.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                String element[] = (String[]) pcm.getElementAt(e.getFirstIndex());
                System.out.println(element[0] + " : " + element[1] + " : " + element[2]);
            }
        }
    });
    JScrollPane jsp = new JScrollPane(jl);
    JComboBox jc = new JComboBox(pcm);
    jc.setRenderer(lcr);
    JButton jb = new JButton("Add Merchandise");
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            pcm.addElement(partsList[(int) (Math.random() * partsList.length)]);
        }
    });
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.NORTH);
    c.add(jc, BorderLayout.CENTER);
    c.add(jb, BorderLayout.SOUTH);
    f.setSize(250, 250);
    f.show();
}

From source file:fr.free.hd.servers.gui.PhonemView.java

@Override
protected JComponent createControl() {
    final JPanel view = new JPanel(new BorderLayout());

    Collection<Phonem> phonesList = phonemsDAO.getPhonems();
    Map<String, Phonem> mapList = new HashMap<String, Phonem>();
    for (Phonem phonem : phonesList) {
        mapList.put(phonem.getPhonem(), phonem);
    }/*from w  w w.  j ava 2s. c  o  m*/

    final StatementListModel model = new StatementListModel(mapList);

    printCommand.setModel(model);
    printCommand.setFace(face);
    copyCommand.setModel(model);
    copyCommand.setFace(face);

    list = new JList(model);
    final JScrollPane sp = new JScrollPane(list);
    final JTextField field = new JTextField();
    field.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void changedUpdate(DocumentEvent e) {
            model.setString(field.getText());
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            model.setString(field.getText());
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            model.setString(field.getText());
        }

    });

    final PhonemListModel phonemModel = new PhonemListModel((List<Phonem>) phonesList);
    final JList phonemList = new JList(phonemModel);
    final JScrollPane spPhonemList = new JScrollPane(phonemList);
    phonemList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        // private int oldIndex = -1;
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting() == false) {
                Phonem innerPhonem = (Phonem) phonemModel.getElementAt(phonemList.getSelectedIndex());
                field.setText(field.getText() + innerPhonem.getPhonem());
            }
        }
    });
    phonemList.setCellRenderer(new PhonemListRenderer());
    list.setCellRenderer(new StatementPhonemListRenderer(face));
    list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    list.setVisibleRowCount(1);

    view.add(spPhonemList, BorderLayout.WEST);
    view.add(sp, BorderLayout.CENTER);
    view.add(field, BorderLayout.SOUTH);

    field.requestFocus();

    return view;
}

From source file:fr.free.hd.servers.gui.FaceView.java

private JList CreateList() {
    Collection<Face> faces = facesDAO.getFaces();
    final JList list = new JList(faces.toArray());
    list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

        @Override/*from   w  ww.  j a v a2 s .com*/
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting() == false) {
                face = (Face) list.getSelectedValue();
            }
        }

    });
    return list;
}

From source file:fr.free.hd.servers.gui.FaceView.java

@Override
protected JComponent createControl() {
    final GridBagLayout layout = new GridBagLayout();
    final JPanel view = new JPanel(layout);

    //Face list//ww w  . j  a  v a2s. c o  m
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 3;
    c.weighty = 0.75;
    c.weightx = 0.15;
    c.fill = GridBagConstraints.BOTH;
    final JList facesList = CreateList();
    facesList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting() == false) {
                if (facesList.getSelectedIndex() != -1) {
                    face = (Face) facesList.getSelectedValue();
                    updateLabel();
                } else {

                }
            }
        }
    });
    view.add(facesList, c);

    // New button
    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 3;
    c.fill = GridBagConstraints.BOTH;
    JButton btnNew = new JButton("Nouveau");
    btnNew.setEnabled(false);
    view.add(btnNew, c);

    // Save button
    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 4;
    c.fill = GridBagConstraints.BOTH;
    JButton btnModified = new JButton("Modifier");
    btnModified.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            facesDAO.storeFace(face);
        }
    });
    view.add(btnModified, c);

    //Draw Face
    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 0;
    c.gridheight = 5;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.60;
    lblFace = new JLabel();
    view.add(lblFace, c);

    //Hand Panel
    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 0;
    c.weightx = 0.15;
    c.fill = GridBagConstraints.BOTH;
    JPanel pnlHand = createPosition();
    view.add(pnlHand, c);

    //Mouth Panel
    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 1;
    c.weightx = 0.15;
    c.fill = GridBagConstraints.BOTH;
    JPanel pnlKind = createKind();
    view.add(pnlKind, c);

    //Mouth Panel
    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 2;
    c.weightx = 0.15;
    c.fill = GridBagConstraints.BOTH;
    JPanel pnlMouth = createMouth();
    view.add(pnlMouth, c);

    // Picture filename
    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 5;
    c.gridwidth = 2;
    c.fill = GridBagConstraints.BOTH;
    JTextField txtPath = new JTextField();
    view.add(txtPath, c);

    //Browse Button
    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 5;
    c.fill = GridBagConstraints.BOTH;
    JButton btnBrownse = new JButton("Browse");
    view.add(btnBrownse, c);

    //Select default face
    if (facesList.getModel().getSize() > 0) {
        facesList.setSelectedIndex(0);
        position = HandPositionEnum.HAND_POSITION_MENTON;
        kind = HandKeyEnum.HAND_KEY_2V;
    }

    return view;
}

From source file:edu.ku.brc.specify.utilapps.RegisterApp.java

/**
 * /*from  w  ww  . ja  v  a2 s.co  m*/
 */
protected void createUI() {
    JTabbedPane tabbedPane = new JTabbedPane();

    rp = new RegProcessor();
    rp.processSQL();

    tree = new JTree(rp.getRoot(INCL_ANON));
    propsTable = new JTable();

    tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
        @Override
        public void valueChanged(TreeSelectionEvent e) {
            fillPropsTable();
        }
    });
    tree.setCellRenderer(new MyTreeCellRenderer());

    Component topComp = UIHelper.createScrollPane(tree);
    Component botComp = UIHelper.createScrollPane(propsTable);
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topComp, botComp);

    splitPane.setDividerLocation(0.5);
    splitPane.setOneTouchExpandable(true);

    splitPane.setLastDividerLocation(300);
    Dimension minimumSize = new Dimension(200, 400);
    topComp.setMinimumSize(minimumSize);
    botComp.setMinimumSize(minimumSize);

    tabbedPane.add("Registration", splitPane);

    final List<Pair<String, String>> trackDescPairs = rp.getTrackKeyDescPairs();
    Vector<String> trkItems = new Vector<String>();
    for (Pair<String, String> p : trackDescPairs) {
        trkItems.add(p.second);
    }

    //Collections.sort(trkItems);
    final JList list = new JList(trkItems);
    //pb.add(UIHelper.createScrollPane(list), cc.xy(1, 1));
    list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                int inx = list.getSelectedIndex();
                if (inx > -1) {
                    fillUsageItemsList(trackDescPairs.get(inx).first);
                }
            }
        }
    });

    tabbedPane.add("Reg Stats", getStatsPane("Registration", rp.getRegNumHash().values(), "register"));

    DefaultListModel model = new DefaultListModel();
    trackItemsList = new JList(model);

    topComp = UIHelper.createScrollPane(list);
    botComp = UIHelper.createScrollPane(trackItemsList);
    splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topComp, botComp);
    splitPane.setDividerLocation(0.5);
    splitPane.setOneTouchExpandable(true);

    topComp.setMinimumSize(minimumSize);
    botComp.setMinimumSize(minimumSize);

    splitPane.setDividerLocation(0.5);
    trackUsageHash = rp.getTrackCatsHash();
    tabbedPane.add("Usage Tracking", splitPane);

    tabbedPane.add("User Stats", getStatsPane("Tracking", rp.getTrackIdHash().values(), "track"));

    add(tabbedPane, BorderLayout.CENTER);
}