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:MainClass.java

public MainClass() {
    setLayout(new BorderLayout());
    model = new DefaultListModel();
    list = new JList(model);
    JScrollPane pane = new JScrollPane(list);
    JButton addButton = new JButton("Add Element");
    JButton removeButton = new JButton("Remove Element");
    for (int i = 0; i < 15; i++)
        model.addElement("Element " + i);

    addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.addElement("Element " + counter);
            counter++;/*from w  w w.  j  a v  a2  s. c o  m*/
        }
    });
    removeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (model.getSize() > 0)
                model.removeElementAt(0);
        }
    });

    add(pane, BorderLayout.NORTH);
    add(addButton, BorderLayout.WEST);
    add(removeButton, BorderLayout.EAST);
}

From source file:Main.java

Main() {
    comboBox = new JComboBox<>(new String[] { "111", "222", "333" });
    JList<String> list = new JList<>(model = new DefaultListModel<>());
    add(comboBox, BorderLayout.SOUTH);
    add(new JScrollPane(list));

    addKeyBindings((JComponent) getContentPane());
    addKeyBindings(comboBox);//from www .j av a  2s  .  co  m

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}

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());
        }/*from www.  j  a v a2s. c  om*/
    });
    cp.add(new JScrollPane(list), BorderLayout.CENTER);
}

From source file:Main.java

public Main() {
    setLayout(new BorderLayout());

    list = new JList(label);
    JScrollPane pane = new JScrollPane(list);

    DefaultListSelectionModel m = new DefaultListSelectionModel();
    m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    m.setLeadAnchorNotificationEnabled(false);
    list.setSelectionModel(m);//from  www  .  ja v  a  2s.com

    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            System.out.println(e.toString());
        }
    });

    add(pane, BorderLayout.NORTH);
}

From source file:Main.java

public Main() {
    setLayout(new BorderLayout());

    list = new JList(label);
    JScrollPane pane = new JScrollPane(list);

    DefaultListSelectionModel m = new DefaultListSelectionModel();
    m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    m.setLeadAnchorNotificationEnabled(false);
    list.setSelectionModel(m);/*from w w  w.j  ava2  s. c o  m*/

    m.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            System.out.println(e.toString());
        }
    });

    add(pane, BorderLayout.NORTH);
}

From source file:Main.java

public Main() {
    JPanel panel = new JPanel();
    panel.setLayout(null);//  w w w .jav a2 s  . c  om

    model = new DefaultListModel();
    list = new JList(model);
    list.setBounds(150, 30, 220, 150);

    JButton okButton = new JButton("Ok");
    okButton.setBounds(30, 35, 80, 25);

    okButton.addActionListener(this);

    panel.add(okButton);
    panel.add(list);
    add(panel);

    setSize(420, 250);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

From source file:MainClass.java

public MainClass() {
    setLayout(new BorderLayout());

    list = new JList(label);
    JScrollPane pane = new JScrollPane(list);

    DefaultListSelectionModel m = new DefaultListSelectionModel();
    m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    m.setLeadAnchorNotificationEnabled(false);
    list.setSelectionModel(m);// ww w . j a v  a  2  s . co  m

    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            System.out.println(e.toString());
        }
    });

    add(pane, BorderLayout.NORTH);
}

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());
        }/*  ww w .j ava2 s . c  o  m*/
    });
    cp.add(new JScrollPane(list), BorderLayout.CENTER);
}

From source file:MainClass.java

MainClass() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Vector v = new Vector();
    v.add("First item");
    v.add("Second item");
    v.add("Third item");
    v.add("Fourth item");

    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(200, 100));
    JList jl = new JList(v);
    jl.setPreferredSize(new Dimension(100, 75));
    p.add(new JScrollPane(jl));

    getContentPane().add(p);// w  w  w .  j  a v  a  2s .c o  m

    pack();
    setVisible(true);
}

From source file:Main.java

public Main() {
    DefaultListModel<String> model = new DefaultListModel<>();
    model.addElement("one");
    model.addElement("two");
    model.addElement("three");
    model.addElement("four");
    model.addElement("five");
    model.addElement("six");
    model.addElement("seven");
    model.addElement("eight");
    model.addElement("nine");
    model.addElement("ten");

    JList<String> list = new JList(model) {
        public String getToolTipText(MouseEvent e) {
            int row = locationToIndex(e.getPoint());
            Object o = getModel().getElementAt(row);
            return o.toString();
        }/*  ww w . ja  va 2 s . com*/

        public Point getToolTipLocation(MouseEvent e) {
            int row = locationToIndex(e.getPoint());
            Rectangle r = getCellBounds(row, row);
            return new Point(r.width, r.y);
        }
    };

    JScrollPane scrollPane = new JScrollPane(list);
    getContentPane().add(scrollPane);
}