Example usage for javax.swing DefaultListModel add

List of usage examples for javax.swing DefaultListModel add

Introduction

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

Prototype

public void add(int index, E element) 

Source Link

Document

Inserts the specified element at the specified position in this list.

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;/*  w w  w.  j  a va 2 s.  c o m*/
    model.add(pos, "a");
}

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;/*w w  w.java 2 s.  c  o  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) {
    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]);

    JList<String> lstFruitList = new JList(dlm);
    lstFruitList.setVisible(true);/*from  w ww  .  j  a  v a2 s.  com*/

    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[] 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]);

    }/*from  w  w w .jav  a2  s .com*/
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    String path = "C:/Pictures/";
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();
    DefaultListModel listModel = new DefaultListModel();
    int count = 0;
    for (int i = 0; i < listOfFiles.length; i++) {
        String name = listOfFiles[i].toString();
        if (name.endsWith("jpg")) {
            ImageIcon ii = new ImageIcon(ImageIO.read(listOfFiles[i]));
            listModel.add(count++, ii);
        }//  ww w  . java2s. c  o m
    }
    JList lsm = new JList(listModel);
    lsm.setVisibleRowCount(1);

    frame.add(new JScrollPane(lsm));

    frame.pack();
    frame.setVisible(true);
}

From source file:ModifyModelSampleModelAction.java

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

    // Fill model
    final DefaultListModel model = new DefaultListModel();
    for (int i = 0, n = labels.length; i < n; i++) {
        model.addElement(labels[i]);// w w w .  j  a v a  2 s  .c  om
    }
    JList jlist = new JList(model);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1, BorderLayout.CENTER);

    JButton jb = new JButton("add F");

    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.add(0, "First");
            model.clear();
            model.addElement("Last");
            model.addElement("Last");
            model.addElement("Last");
            model.addElement("Last");

            int size = model.getSize();
            model.insertElementAt("Middle", size / 2);
            model.set(0, "New First");
            model.setElementAt("New Last", size - 1);

            model.remove(0);
            // model.removeAllElements();
            //    model.removeElement("Last");
            //      model.removeElementAt(size / 2);
            //        model.removeRange(0, size / 2);
        }
    });

    frame.add(jb, BorderLayout.SOUTH);

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

From source file:DnDDemo3.java

public static void main(String[] args) {
    JPanel north = new JPanel();
    north.add(new JLabel("Drag from here:"));
    JTextField field = new JTextField(10);
    field.setDragEnabled(true);/*from   w  ww . j a  v a2s  .co  m*/
    north.add(field);

    final DefaultListModel listModel = new DefaultListModel();
    listModel.addElement("first");
    listModel.addElement("second");
    final JList list = new JList(listModel);
    list.setDragEnabled(true);

    list.setTransferHandler(new TransferHandler() {
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return false;
            }
            JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
            if (dl.getIndex() == -1) {
                return false;
            } else {
                return true;
            }
        }

        public boolean importData(TransferHandler.TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }

            Transferable transferable = support.getTransferable();
            String data;
            try {
                data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            } catch (Exception e) {
                return false;
            }

            JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
            int index = dl.getIndex();
            if (dl.isInsert()) {
                listModel.add(index, data);
            } else {
                listModel.set(index, data);
            }
            Rectangle r = list.getCellBounds(index, index);
            list.scrollRectToVisible(r);
            return true;
        }
    });
    JScrollPane center = new JScrollPane();
    center.setViewportView(list);

    list.setDropMode(DropMode.USE_SELECTION);
    JPanel cp = new JPanel();
    cp.setLayout(new BorderLayout());
    cp.add(north, BorderLayout.NORTH);
    cp.add(center, BorderLayout.CENTER);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(cp);
    frame.pack();
    frame.setVisible(true);
}

From source file:DropModeON.java

public static void main(String[] args) {
    JPanel north = new JPanel();
    north.add(new JLabel("Drag from here:"));
    JTextField field = new JTextField(10);
    field.setDragEnabled(true);//from   w  w w.  j ava 2 s .c om
    north.add(field);

    final DefaultListModel listModel = new DefaultListModel();
    listModel.addElement("first");
    listModel.addElement("second");
    final JList list = new JList(listModel);
    list.setDragEnabled(true);

    list.setTransferHandler(new TransferHandler() {
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return false;
            }
            JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
            if (dl.getIndex() == -1) {
                return false;
            } else {
                return true;
            }
        }

        public boolean importData(TransferHandler.TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }

            Transferable transferable = support.getTransferable();
            String data;
            try {
                data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            } catch (Exception e) {
                return false;
            }

            JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
            int index = dl.getIndex();
            if (dl.isInsert()) {
                listModel.add(index, data);
            } else {
                listModel.set(index, data);
            }

            // Scroll to display the element that was dropped
            Rectangle r = list.getCellBounds(index, index);
            list.scrollRectToVisible(r);
            return true;
        }
    });
    JScrollPane center = new JScrollPane();
    center.setViewportView(list);

    list.setDropMode(DropMode.ON);
    JPanel cp = new JPanel();
    cp.setLayout(new BorderLayout());
    cp.add(north, BorderLayout.NORTH);
    cp.add(center, BorderLayout.CENTER);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(cp);
    frame.pack();
    frame.setVisible(true);
}

From source file:DropModeON_OR_INSERT.java

public static void main(String[] args) {
    JPanel north = new JPanel();
    north.add(new JLabel("Drag from here:"));
    JTextField field = new JTextField(10);
    field.setDragEnabled(true);/*from   ww  w . j a v  a 2s  .  com*/
    north.add(field);

    final DefaultListModel listModel = new DefaultListModel();
    listModel.addElement("first");
    listModel.addElement("second");
    final JList list = new JList(listModel);
    list.setDragEnabled(true);

    list.setTransferHandler(new TransferHandler() {
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return false;
            }
            JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
            if (dl.getIndex() == -1) {
                return false;
            } else {
                return true;
            }
        }

        public boolean importData(TransferHandler.TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }

            Transferable transferable = support.getTransferable();
            String data;
            try {
                data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            } catch (Exception e) {
                return false;
            }

            JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
            int index = dl.getIndex();
            if (dl.isInsert()) {
                listModel.add(index, data);
            } else {
                listModel.set(index, data);
            }

            // Scroll to display the element that was dropped
            Rectangle r = list.getCellBounds(index, index);
            list.scrollRectToVisible(r);
            return true;
        }
    });
    JScrollPane center = new JScrollPane();
    center.setViewportView(list);

    list.setDropMode(DropMode.ON_OR_INSERT);
    JPanel cp = new JPanel();
    cp.setLayout(new BorderLayout());
    cp.add(north, BorderLayout.NORTH);
    cp.add(center, BorderLayout.CENTER);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(cp);
    frame.pack();
    frame.setVisible(true);
}