Example usage for javax.swing DefaultListModel DefaultListModel

List of usage examples for javax.swing DefaultListModel DefaultListModel

Introduction

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

Prototype

DefaultListModel

Source Link

Usage

From source file:Main.java

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

    int pos = 0;//from   w  ww  .  j  a  v a2 s. c o m
    model.add(pos, "a");
}

From source file:Main.java

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

    // Replace the 2nd item
    int pos = 1;//from w  w w.  j  av a 2s  .c o m
    model.set(pos, "b");
}

From source file:Main.java

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

    // Initialize the list with items
    String[] items = { "A", "B", "C", "D" };
    for (int i = 0; i < items.length; i++) {
        model.add(i, items[i]);/*ww w .ja  v a  2  s. c  om*/

    }
}

From source file:Main.java

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

    // Remove the first item
    int pos = 0;/*from  w w w .  java  2s  .c o m*/
    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[] argv) throws Exception {
    // Create a list that allows adds and removes
    DefaultListModel model = new DefaultListModel();
    JList list = new JList(model);

    int pos = list.getModel().getSize();
    model.add(pos, "E");
}

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  w  ww. j a va 2 s  . c om
    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) {
    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]);/*  w w  w .  j ava 2 s  .  c  o  m*/

    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) {
    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  av  a  2  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  .  ja v  a 2 s  .  c  o  m

    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[] arg) {
    DefaultListModel<String> listModel = new DefaultListModel<String>();
    for (int i = 0; i < 10; i++) {
        listModel.addElement("Item " + (i + 1));
    }/*ww  w  . j  a v a 2  s .c  o m*/

    JList<String> list = new JList<String>(listModel);
    list.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent me) {
            if (SwingUtilities.isRightMouseButton(me)) {
                list.clearSelection();
            }
        }
    });

    JScrollPane listScrollPane = new JScrollPane(list);
    JFrame f = new JFrame();
    f.getContentPane().add(listScrollPane);

    f.setSize(500, 400);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}