Example usage for javax.swing DefaultListModel removeElement

List of usage examples for javax.swing DefaultListModel removeElement

Introduction

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

Prototype

public boolean removeElement(Object obj) 

Source Link

Document

Removes the first (lowest-indexed) occurrence of the argument from this list.

Usage

From source file:Main.java

public static void main(final String args[]) {
    String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(1000);/*www . j av  a  2  s  . c o m*/
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 5; j++) {
            model.addElement(labels[j]);
        }
    }
    model.removeElement("A");

    JList jlist2 = new JList(model);
    jlist2.setVisibleRowCount(4);
    jlist2.setFixedCellHeight(12);
    jlist2.setFixedCellWidth(200);
    JScrollPane scrollPane2 = new JScrollPane(jlist2);
    frame.add(scrollPane2, BorderLayout.CENTER);

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

From source file:MainClass.java

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

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

    ListDataListener listDataListener = new ListDataListener() {
        public void contentsChanged(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        public void intervalAdded(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        public void intervalRemoved(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        private void appendEvent(ListDataEvent listDataEvent) {
            switch (listDataEvent.getType()) {
            case ListDataEvent.CONTENTS_CHANGED:
                System.out.println("Type: Contents Changed");
                break;
            case ListDataEvent.INTERVAL_ADDED:
                System.out.println("Type: Interval Added");
                break;
            case ListDataEvent.INTERVAL_REMOVED:
                System.out.println("Type: Interval Removed");
                break;
            }
            System.out.println(", Index0: " + listDataEvent.getIndex0());
            System.out.println(", Index1: " + listDataEvent.getIndex1());
            DefaultListModel theModel = (DefaultListModel) listDataEvent.getSource();
            System.out.println(theModel);
        }
    };

    model.addListDataListener(listDataListener);

    model.add(0, "First");
    model.addElement("Last");
    int size = model.getSize();
    model.insertElementAt("Middle", size / 2);
    size = model.getSize();
    if (size != 0)
        model.set(0, "New First");
    size = model.getSize();
    if (size != 0)
        model.setElementAt("New Last", size - 1);
    for (int i = 0, n = labels.length; i < n; i++) {
        model.addElement(labels[i]);
    }
    model.clear();
    size = model.getSize();
    if (size != 0)
        model.remove(0);

    model.removeAllElements();
    model.removeElement("Last");
    size = model.getSize();
    if (size != 0)
        model.removeElementAt(size / 2);
    size = model.getSize();
    if (size != 0)
        model.removeRange(0, size / 2);
    frame.setSize(640, 300);
    frame.setVisible(true);
}

From source file:ModifyModelSample.java

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

    // Fill model
    final DefaultListModel model = new DefaultListModel();
    for (int i = 0, n = labels.length; i < n; i++) {
        model.addElement(labels[i]);//  ww w  .jav  a2s .c  o  m
    }
    JList jlist = new JList(model);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    contentPane.add(scrollPane1, BorderLayout.WEST);

    final JTextArea textArea = new JTextArea();
    textArea.setEditable(false);
    JScrollPane scrollPane2 = new JScrollPane(textArea);
    contentPane.add(scrollPane2, BorderLayout.CENTER);

    ListDataListener listDataListener = new ListDataListener() {
        public void contentsChanged(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        public void intervalAdded(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        public void intervalRemoved(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        private void appendEvent(ListDataEvent listDataEvent) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            switch (listDataEvent.getType()) {
            case ListDataEvent.CONTENTS_CHANGED:
                pw.print("Type: Contents Changed");
                break;
            case ListDataEvent.INTERVAL_ADDED:
                pw.print("Type: Interval Added");
                break;
            case ListDataEvent.INTERVAL_REMOVED:
                pw.print("Type: Interval Removed");
                break;
            }
            pw.print(", Index0: " + listDataEvent.getIndex0());
            pw.print(", Index1: " + listDataEvent.getIndex1());
            DefaultListModel theModel = (DefaultListModel) listDataEvent.getSource();
            Enumeration elements = theModel.elements();
            pw.print(", Elements: ");
            while (elements.hasMoreElements()) {
                pw.print(elements.nextElement());
                pw.print(",");
            }
            pw.println();
            textArea.append(sw.toString());
        }
    };

    model.addListDataListener(listDataListener);

    // Setup buttons
    JPanel jp = new JPanel(new GridLayout(2, 1));
    JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1));
    JPanel jp2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1));
    jp.add(jp1);
    jp.add(jp2);
    JButton jb = new JButton("add F");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.add(0, "First");
        }
    });
    jb = new JButton("addElement L");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.addElement("Last");
        }
    });
    jb = new JButton("insertElementAt M");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            model.insertElementAt("Middle", size / 2);
        }
    });
    jb = new JButton("set F");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.set(0, "New First");
        }
    });
    jb = new JButton("setElementAt L");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.setElementAt("New Last", size - 1);
        }
    });
    jb = new JButton("load 10");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            for (int i = 0, n = labels.length; i < n; i++) {
                model.addElement(labels[i]);
            }
        }
    });
    jb = new JButton("clear");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.clear();
        }
    });
    jb = new JButton("remove F");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.remove(0);
        }
    });
    jb = new JButton("removeAllElements");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.removeAllElements();
        }
    });
    jb = new JButton("removeElement 'Last'");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.removeElement("Last");
        }
    });
    jb = new JButton("removeElementAt M");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.removeElementAt(size / 2);
        }
    });
    jb = new JButton("removeRange FM");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.removeRange(0, size / 2);
        }
    });
    contentPane.add(jp, BorderLayout.SOUTH);
    frame.setSize(640, 300);
    frame.setVisible(true);
}

From source file:Main.java

@Override
protected void exportDone(JComponent source, Transferable data, int action) {
    @SuppressWarnings("unchecked")
    JList<String> sourceList = (JList<String>) source;
    String movedItem = sourceList.getSelectedValue();
    if (action == TransferHandler.MOVE) {
        DefaultListModel<String> listModel = (DefaultListModel<String>) sourceList.getModel();
        listModel.removeElement(movedItem);
    }/*w ww.ja v a  2 s. c  om*/
}

From source file:edu.ku.brc.af.tasks.subpane.formeditor.RowColDefPanel.java

public void delItem() {
    if (!currentItem.isInUse()) {
        DefaultListModel model = (DefaultListModel) itemList.getModel();
        items.remove(currentItem);// w  ww. ja  v  a2s .  co  m
        model.removeElement(currentItem);
    }
}

From source file:edu.ku.brc.af.ui.forms.formatters.UIFormatterListEdtDlg.java

/**
 * /*from   w  w  w .j a va2 s .  c  om*/
 */
protected void deleteFormatter() {
    UIFieldFormatterIFace uif = (UIFieldFormatterIFace) formatList.getSelectedValue();
    if (uif != null) {
        DefaultListModel model = (DefaultListModel) formatList.getModel();
        model.removeElement(uif);
        uiFieldFormatterMgrCache.removeFormatter(uif);
        setHasChanged(true);
    }
}

From source file:edu.ku.brc.af.ui.forms.formatters.UIFormatterListEdtDlg.java

/**
 * @param uif/*from w  w w .  j  av  a2 s . c o m*/
 * @param isNew
 */
protected void editFormatter(final UIFieldFormatterIFace uif, final boolean isNew) {
    try {
        UIFieldFormatterIFace tempCopy = isNew ? uif : (UIFieldFormatterIFace) uif.clone();
        boolean isDefault = tempCopy.isDefault();

        UIFormatterEditorDlg dlg = new UIFormatterEditorDlg(this, fieldInfo, tempCopy, isNew, doProcessSamples,
                uiFieldFormatterMgrCache);
        dlg.setVisible(true);
        if (!dlg.isCancelled()) {
            UIFieldFormatterIFace selectedUIF = dlg.getSelectedFormat();
            selectedUIF.setDefault(isDefault);

            if (selectedUIF.isIncrementer() && selectedUIF.getAutoNumber() == null) {

                AutoNumberIFace autoNum = UIFieldFormatterMgr.getInstance().createAutoNumber(
                        AutoNumberGeneric.class.getName(), fieldInfo.getTableInfo().getClassName(),
                        fieldInfo.getName(), tempCopy.getFields().size() == 1);
                selectedUIF.setAutoNumber(autoNum);
            }

            DefaultListModel model = (DefaultListModel) formatList.getModel();
            if (isNew) {
                model.addElement(selectedUIF);
                uiFieldFormatterMgrCache.addFormatter(selectedUIF);
                if (model.size() == 1) {
                    selectedUIF.setDefault(true);
                    formatList.setSelectedValue(selectedUIF, true);
                }

            } else {
                model.removeElement(uif);
                model.addElement(selectedUIF);
                uiFieldFormatterMgrCache.removeFormatter(uif);
                uiFieldFormatterMgrCache.addFormatter(selectedUIF);
            }
            setHasChanged(true);

        }

    } catch (Exception ex) {
        ex.printStackTrace();
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(UIFormatterListEdtDlg.class, ex);
    }
}

From source file:gtu._work.ui.ObnfInsertCreaterUI.java

private void deleteFieldBtnAction() {
    String dbField = dbFieldText.getText();
    if (StringUtils.isBlank(dbField)) {
        JCommonUtil._jOptionPane_showMessageDialog_error("dbField");
        return;// w w w . j a  v a  2  s . c  o  m
    }
    DefaultListModel dbFieldListModel = (DefaultListModel) dbFieldList.getModel();
    boolean deleteOk = false;
    for (Enumeration<?> enu = dbFieldListModel.elements(); enu.hasMoreElements();) {
        KeyValue kv = (KeyValue) enu.nextElement();
        if (kv.getKey().equalsIgnoreCase(dbField)) {
            dbFieldListModel.removeElement(kv);
            deleteOk = true;
            break;
        }
    }
    JCommonUtil._jOptionPane_showMessageDialog_info(
            ":" + dbField + "->" + (deleteOk ? "?" : "?"));
}

From source file:com.mirth.connect.client.ui.browsers.message.MessageBrowserAdvancedFilter.java

private void shiftValues(JList source, JList destination) {
    Object[] values = source.getSelectedValues();
    DefaultListModel sourceModel = (DefaultListModel) source.getModel();
    DefaultListModel destinationModel = (DefaultListModel) destination.getModel();

    for (Object value : values) {
        sourceModel.removeElement(value);
        destinationModel.addElement(value);
    }//w ww.j av  a2s. c  o m
}

From source file:de.tor.tribes.ui.views.DSWorkbenchReTimerFrame.java

private void fireRemoveTroopFilterEvent(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fireRemoveTroopFilterEvent
    Object[] selection = jFilterList.getSelectedValues();
    if (selection == null || selection.length == 0) {
        return;/* w  w w .  jav  a2s  .co m*/
    }
    List<TroopFilterElement> toRemove = new LinkedList<TroopFilterElement>();
    for (Object elem : selection) {
        toRemove.add((TroopFilterElement) elem);
    }
    DefaultListModel filterModel = (DefaultListModel) jFilterList.getModel();
    for (TroopFilterElement elem : toRemove) {
        filterModel.removeElement(elem);
    }
    jFilterList.repaint();
}