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.addListSelectionListener(new MyListSelectionListener());
}

From source file:Main.java

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

    int pos = 0;//from   ww w. ja v a 2s  .  co m
    model.add(pos, "a");

    //Insert an item at the beginning
    model.add(pos, "a");

    // Initialize the list with items
    String[] items = { "A", "B", "C", "D" };
    for (int i = 0; i < items.length; i++) {
        model.add(i, items[i]);
    }

    // Replace the 2nd item
    pos = 1;
    model.set(pos, "b");

    // Remove the first item
    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) {
    JList<String> list = new JList<String>(new String[] { "one", "two", "three", "four", "five" });
    JScrollPane scrollPane = new JScrollPane(list);
    JButton btn = new JButton(new AbstractAction() {
        {/*from  ww w  .  j av a  2  s  .  c o  m*/
            putValue(NAME, "Select");
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            list.setSelectedIndex(0);
        }
    });
    JPanel panel = new JPanel();
    panel.add(scrollPane);
    panel.add(btn);
    JOptionPane.showMessageDialog(null, panel);
}

From source file:Main.java

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

    Vector months = new Vector();
    JList list = new JList(months);

    months.addElement("January");
    months.addElement("December");

    frame.add(new JScrollPane(list));

    frame.setSize(300, 200);/* w  ww  . ja  v a  2 s.  c  om*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    int bigNumber = 30001;
    String[] bigData = new String[bigNumber];
    for (int ii = 0; ii < bigNumber; ii++) {
        bigData[ii] = "String " + (ii + 1);
    }//from   ww w.ja  v a  2s  .  com
    JList list = new JList(bigData);
    list.setVisibleRowCount(5);

    JOptionPane.showMessageDialog(null, new JScrollPane(list));
}

From source file:Main.java

public static void main(String[] args) {
    DefaultListModel<String> model = new DefaultListModel<>();
    JList<String> sList = new JList<>(model);
    for (int i = 0; i < 100; i++) {
        model.addElement("String " + i);
    }//w  w w .j  a v a2  s.  c o  m

    sList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    sList.setVisibleRowCount(-1);
    sList.setLayoutOrientation(JList.HORIZONTAL_WRAP);

    JFrame frame = new JFrame("Foo001");
    frame.getContentPane().add(new JScrollPane(sList));
    frame.getContentPane().setPreferredSize(new Dimension(400, 300));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    int MAX_CELLS = 30;

    DefaultListModel<String> listModel = new DefaultListModel<>();
    JList<String> myList = new JList<>(listModel);
    myList.setVisibleRowCount(8);//from   ww w  . j  a va 2  s. c om

    for (int i = 0; i < MAX_CELLS; i++) {
        listModel.addElement("label " + i);
    }

    JTabbedPane jTabbedPane = new JTabbedPane();

    jTabbedPane.add("Test", new JScrollPane(myList));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(jTabbedPane);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String[] items = { "item1", "item2", "item1" };
    JList<String> list = new JList<>(items);
    JTextField output = new JTextField(15);
    JPanel gui = new JPanel();
    gui.add(list);// w  w w. j a  v  a 2s.c  o m
    gui.add(output);
    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent lse) {
            int index = list.getSelectedIndex();
            String outputText = "Index: " + index + "  Value: " + items[index];
            output.setText(outputText);
        }
    });
    JOptionPane.showMessageDialog(null, gui);
}

From source file:Main.java

public static void main(String[] args) {
    String[] items = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
    JList<String> myJList = new JList(items) {
        @Override/*w  ww .  ja  va 2s  .c  o  m*/
        protected void processMouseEvent(MouseEvent e) {
            int modifiers = e.getModifiers() | InputEvent.CTRL_MASK;
            int modifiersEx = e.getModifiersEx() | InputEvent.CTRL_MASK;
            MouseEvent myME = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), modifiers,
                    e.getX(), e.getY(), e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(),
                    e.isPopupTrigger(), e.getButton());
            super.processMouseEvent(myME);
        }
    };
    JFrame f = new JFrame();
    f.add(new JScrollPane(myJList));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setLocationRelativeTo(null);
    f.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(labels);
    jlist1.setVisibleRowCount(4);/*  w  w w.  j a  v a 2  s  . com*/
    JScrollPane scrollPane1 = new JScrollPane(jlist1);
    frame.add(scrollPane1, BorderLayout.NORTH);

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