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

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton jButton1 = new JButton("Button");

    String[] mystring = { "Java", "JBuilder", "JFC", "Swing" };
    final JList jList1 = new JList(mystring);
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Object contents = jList1.getSelectedValue();
            System.out.println(contents);
        }/*from   w  ww.  j a  v a2s  .  com*/
    });

    frame.add(jList1, "Center");
    frame.add(jButton1, "South");

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

From source file:DualSample.java

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };

    JFrame f = new JFrame("Sample Components");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JList list = new JList(labels);
    JScrollPane scrollPane = new JScrollPane(list);

    JComboBox comboBox = new JComboBox(labels);
    comboBox.setMaximumRowCount(4);/*  ww w .ja v  a  2s. c o m*/

    Container contentPane = f.getContentPane();
    contentPane.add(comboBox, BorderLayout.NORTH);
    contentPane.add(scrollPane, BorderLayout.CENTER);

    f.setSize(300, 200);
    f.setVisible(true);
}

From source file:Main.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);

    final JList jlist = new JList(labels);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1);/*from  w  ww  . j  a v  a2  s .c o m*/

    ListSelectionListener listSelectionListener = new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent listSelectionEvent) {
            System.out.println(jlist.getSelectedValue());
        }
    };
    jlist.addListSelectionListener(listSelectionListener);

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

From source file:Main.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);

    final JList jlist = new JList(labels);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1);//from   w  ww  .  j a va2s. c  om

    ListSelectionListener listSelectionListener = new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent listSelectionEvent) {
            Object[] objs = jlist.getSelectedValues();
        }
    };
    jlist.addListSelectionListener(listSelectionListener);

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

From source file:Main.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);

    final JList jlist = new JList(labels);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1);/*from  ww  w  . ja  va  2  s .  c o m*/

    ListSelectionListener listSelectionListener = new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent listSelectionEvent) {
            System.out.println(jlist.getSelectedIndex());
        }
    };
    jlist.addListSelectionListener(listSelectionListener);

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

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String[] selections = { "green", "red", "orange", "dark blue" };
    JList list = new JList(selections) {
        // This method is called as the cursor moves within the list.
        public String getToolTipText(MouseEvent evt) {
            // Get item index
            int index = locationToIndex(evt.getPoint());

            // Get item
            Object item = getModel().getElementAt(index);

            // Return the tool tip text
            return "tool tip for " + item;
        }//  w w w . jav a  2 s  .  c  o  m
    };
    list.setSelectedIndex(1);
    System.out.println(list.getSelectedValue());
    frame.add(new JScrollPane(list));
    frame.pack();

    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    DefaultListModel<String> dlm = new DefaultListModel();
    String[] modelElems = { "Apple", "Orange", "Banana" };
    for (int i = 0; i < modelElems.length; i++)
        dlm.add(i, modelElems[i]);/*from   w  w  w  .j  av  a2 s  .c om*/

    JList<String> lstFruitList = new JList(dlm);
    lstFruitList.setVisible(true);

    JPanel p = new JPanel();
    p.add(lstFruitList);
    f.add(p);
    f.setLocation(0, 0);
    f.setSize(400, 400);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(800, 600));

    String test[] = { "alpha", "bravo", "charlie", "delta", "echo", "omega", "zeta" };
    JList<String> list = new JList<>(test);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setLayoutOrientation(JList.VERTICAL);
    list.setVisibleRowCount(5);/* w  w  w .  j a  v a2 s.co  m*/
    list.setBounds(50, 150, 75, 90);

    JScrollPane jScrollPane1 = new JScrollPane();
    jScrollPane1.setViewportView(list);

    panel.add(jScrollPane1);

    JFrame f = new JFrame();
    f.add(panel);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setResizable(false);
    f.setFocusable(true);
}

From source file:SizingSamples.java

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };

    JFrame frame = new JFrame("Sizing Samples");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JList jlist1 = new JList(labels);
    jlist1.setVisibleRowCount(4);/*from ww w  .  jav a 2 s  .co m*/
    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(100);
    for (int i = 0; i < 100; i++) {
        model.addElement(Integer.toString(i));
    }
    JScrollPane scrollPane1 = new JScrollPane(jlist1);
    frame.add(scrollPane1, BorderLayout.NORTH);

    JList jlist2 = new JList(model);
    jlist2.setVisibleRowCount(4);
    jlist2.setFixedCellHeight(12);
    jlist2.setFixedCellWidth(200);

    JScrollPane scrollPane2 = new JScrollPane(jlist2);
    frame.add(scrollPane2, BorderLayout.CENTER);

    JList jlist3 = new JList(labels);
    jlist3.setVisibleRowCount(4);
    frame.add(jlist3, BorderLayout.SOUTH);

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

From source file:JListTest.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JList Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String[] selections = { "green", "red", "orange", "dark blue" };
    JList list = new JList(selections);
    list.setSelectedIndex(1);/*from  w w  w .ja  v  a  2  s  . c  om*/
    System.out.println(list.getSelectedValue());
    frame.add(new JScrollPane(list));
    frame.pack();

    frame.setVisible(true);
}