Example usage for javax.swing DefaultListModel elements

List of usage examples for javax.swing DefaultListModel elements

Introduction

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

Prototype

public Enumeration<E> elements() 

Source Link

Document

Returns an enumeration of the components of 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);// w w  w .j  a  v a 2s.  c o m
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 5; j++) {
            model.addElement(labels[j]);
        }
    }
    Enumeration<Object> e = model.elements();

    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: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]);//from ww w .ja va2 s.com
    }
    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:de.pavloff.spark4knime.jsnippet.ui.JarListPanel.java

private void onJarFileAdd() {
    DefaultListModel<String> model = (DefaultListModel<String>) m_addJarList.getModel();
    Set<String> hash = new HashSet<>(Collections.list(model.elements()));
    StringHistory history = StringHistory.getInstance("java_snippet_jar_dirs");
    if (m_jarFileChooser == null) {
        File dir = null;//from  w  w w . j a  va2  s .c om
        for (String h : history.getHistory()) {
            File temp = new File(h);
            if (temp.isDirectory()) {
                dir = temp;
                break;
            }
        }
        m_jarFileChooser = new JFileChooser(dir);
        m_jarFileChooser.setFileFilter(new SimpleFileFilter(".zip", ".jar"));
        m_jarFileChooser.setMultiSelectionEnabled(true);
    }
    int result = m_jarFileChooser.showDialog(m_addJarList, "Select");

    if (result == JFileChooser.APPROVE_OPTION) {
        for (File f : m_jarFileChooser.getSelectedFiles()) {
            String s = f.getAbsolutePath();
            if (hash.add(s)) {
                model.addElement(s);
            }
        }
        history.add(m_jarFileChooser.getCurrentDirectory().getAbsolutePath());
    }
}

From source file:de.pavloff.spark4knime.jsnippet.ui.JarListPanel.java

@SuppressWarnings("null")
private void onJarURLAdd() {
    DefaultListModel<String> model = (DefaultListModel<String>) m_addJarList.getModel();
    Set<String> hash = new HashSet<>(Collections.list(model.elements()));
    String input = "knime://knime.workflow/example.jar";
    boolean valid;
    do {/*from   w  ww .j a v a  2s  .  c om*/
        input = JOptionPane.showInputDialog(this, "Enter a \"knime:\" URL to a JAR file", input);
        if (StringUtils.isEmpty(input)) {
            valid = true;
        } else {
            URL url;
            try {
                url = new URL(input);
                File file = FileUtil.getFileFromURL(url);
                CheckUtils.checkArgument(file != null, "Could not resolve to local file");
                CheckUtils.checkArgument(file.exists(), "File does not exists; location resolved to\n%s",
                        file.getAbsolutePath());
                valid = true;
            } catch (MalformedURLException | IllegalArgumentException mfe) {
                JOptionPane.showMessageDialog(this, "Invalid URL\n" + mfe.getMessage(), "Error parsing URL",
                        JOptionPane.ERROR_MESSAGE);
                valid = false;
                continue;
            }
        }
    } while (!valid);
    if (!StringUtils.isEmpty(input) && hash.add(input)) {
        model.addElement(input);
    }
}

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

private void jTextField2filterAgain(boolean resetGroupListModel) {
    try {/*w  w  w . j  a  v a 2 s  .  c o m*/
        Pattern pat = Pattern.compile(contentFilter.getText());
        Matcher matcher = null;
        DefaultListModel listModel = new DefaultListModel();
        Set<String> set = new HashSet<String>();
        boolean setGroupListModel = false;
        for (String str : contentSet) {
            matcher = pat.matcher(replaceNewLineChar(str));
            if (contextFilter.getSelectedItem().equals("All")) {
                if (matcher.find()) {
                    listModel.addElement(str);
                }
            } else if (contextFilter.getSelectedItem().equals("Match")) {
                List<String> mlist = new ArrayList<String>();
                while (matcher.find()) {
                    for (int ii = 0; ii <= matcher.groupCount(); ii++) {
                        if (!resetGroupListModel) {
                            DefaultListModel model = (DefaultListModel) groupList.getModel();
                            for (Enumeration<?> enu = model.elements(); enu.hasMoreElements();) {
                                Integer gnum = (Integer) enu.nextElement();
                                if (gnum == ii) {
                                    mlist.add(matcher.group(ii));
                                }
                            }
                        } else {
                            mlist.add(matcher.group(ii));
                        }
                    }
                    if (resetGroupListModel && !setGroupListModel) {
                        resetGroupList(matcher.groupCount());
                    }
                }
                set.add(mlist.toString());
            }
        }
        if (contextFilter.getSelectedItem().equals("Match")) {
            List<String> ssset = new ArrayList<String>(set);
            Collections.sort(ssset);
            for (String str : ssset) {
                listModel.addElement(str);
            }
        }
        matchList.setModel(listModel);
        matchLabel.setText(String.valueOf(matchList.getModel().getSize()));
    } catch (Exception ex) {
    }
    sysinfo();
}

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

private void jButton2ActionPerformed(ActionEvent evt) {
    String searchtext = searchText.getText();
    if (StringUtils.isEmpty(searchtext) || searchtext.length() < 2) {
        JOptionPaneUtil.newInstance().iconErrorMessage().showMessageDialog("", "error");
        return;/*from www.  j  av  a2 s .c om*/
    }
    try {
        DefaultListModel model = (DefaultListModel) jarFileDirs.getModel();

        searchtext = searchtext.replace('/', '.');
        searchtext = searchtext.replace('\\', '.');

        jarfinder.pattern(searchtext);

        for (Enumeration<?> enu = model.elements(); enu.hasMoreElements();) {
            File file = (File) enu.nextElement();
            jarfinder.setDir(file).execute();
        }
    } catch (Exception ex) {
        // JOptionPaneUtil.newInstance().iconErrorMessage().showMessageDialog(ex.getMessage(),
        // "error");
        JCommonUtil.handleException(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 .  ja  v  a2  s . co 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:gtu._work.ui.ObnfInsertCreaterUI.java

private void addFieldBtnAction() {
    String dbField = dbFieldText.getText();
    String dbvalue = StringUtils.defaultString(dbValue.getText());
    if (StringUtils.isBlank(dbField)) {
        JCommonUtil._jOptionPane_showMessageDialog_error("dbField");
        return;//from   w  ww. j  a v  a2s.  c  o m
    }
    DefaultListModel dbFieldListModel = (DefaultListModel) dbFieldList.getModel();
    boolean doUpdate = false;
    for (Enumeration<?> enu = dbFieldListModel.elements(); enu.hasMoreElements();) {
        KeyValue kv = (KeyValue) enu.nextElement();
        if (kv.getKey().equalsIgnoreCase(dbField) && kv.pk == pkCheckBox.isSelected()) {
            kv.value = dbvalue;
            doUpdate = true;
            JCommonUtil._jOptionPane_showMessageDialog_info(":" + kv);
            break;
        }
    }
    if (!doUpdate) {
        KeyValue kv = new KeyValue();
        kv.key = dbField;
        kv.value = dbvalue;
        kv.pk = pkCheckBox.isSelected();
        dbFieldListModel.addElement(kv);
        JCommonUtil._jOptionPane_showMessageDialog_info(":" + kv);
    }
}

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

private void processSqlBtnAction() {
    Map<String, String> wkDataObjectMapCopy = new LinkedHashMap<String, String>();
    Map<String, String> wkKeyMapCopy = new LinkedHashMap<String, String>();

    DefaultListModel dbFieldListModel = (DefaultListModel) dbFieldList.getModel();
    for (Enumeration<?> enu = dbFieldListModel.elements(); enu.hasMoreElements();) {
        KeyValue kv = (KeyValue) enu.nextElement();
        if (kv.pk == false) {
            wkDataObjectMapCopy.put(kv.key, kv.value);
        } else {//from  www  .j a  v  a2  s .c  o  m
            wkKeyMapCopy.put(kv.key, kv.value);
        }
    }

    if (useDomainJarDefineChkBox.isSelected()) {
        loadJarConfig();
        StringBuffer sb = new StringBuffer();
        sb.append("PK=>\n");
        this.keepKey(wkKeyMapCopy, test.getPkColumns(), sb);
        sb.append("COLUMN=>\n");
        this.keepKey(wkDataObjectMapCopy, test.getColumns(), sb);
        JCommonUtil._jOptionPane_showMessageDialog_info(sb);
        for (String key : wkKeyMapCopy.keySet()) {
            if (StringUtils.isBlank(wkKeyMapCopy.get(key)) && //
                    wkDataObjectMapCopy.containsKey(key) && //
                    StringUtils.isNotBlank(wkDataObjectMapCopy.get(key))) {
                System.out.println("##pk = " + key + " = " + wkDataObjectMapCopy.get(key));
                wkKeyMapCopy.put(key, wkDataObjectMapCopy.get(key));
            }
        }
    }

    System.out.println("wkKeyMapCopy = " + wkKeyMapCopy);
    System.out.println("wkDataObjectMapCopy = " + wkDataObjectMapCopy);

    String whereCondition = "";
    if (!wkKeyMapCopy.isEmpty()) {
        System.out.println("====> wkKeyMapCopy ? !!!!!!");
        whereCondition = wkKeyMapCopy.toString();
    } else {
        whereCondition = wkDataObjectMapCopy.toString();
    }

    whereCondition = whereCondition.replaceAll(",", "' and ");
    whereCondition = whereCondition.replaceAll("=", "='");
    whereCondition = whereCondition.substring(1);
    whereCondition = whereCondition.substring(0, whereCondition.length() - 1);
    whereCondition = whereCondition + "'";

    String selectSQL = "select * from " + tableName + " where " + whereCondition + ";\n\n";

    String updateSetStr = wkDataObjectMapCopy.toString();
    updateSetStr = updateSetStr.replaceAll(",", "' ,");
    updateSetStr = updateSetStr.replaceAll("=", "='");
    updateSetStr = updateSetStr.substring(1);
    updateSetStr = updateSetStr.substring(0, updateSetStr.length() - 1);
    updateSetStr = updateSetStr + "'";

    String updateSQL = "update " + tableName + " set " + updateSetStr + " where " + whereCondition + ";\n\n";

    List<String> insertFieldList = new ArrayList<String>();
    List<String> insertValueList = new ArrayList<String>();
    for (String key : wkDataObjectMapCopy.keySet()) {
        insertFieldList.add(key);
        insertValueList.add(wkDataObjectMapCopy.get(key));
    }
    String inf = insertFieldList.toString();
    inf = inf.replaceAll(" ", "");
    inf = inf.substring(1, inf.length() - 1);
    String inv = insertValueList.toString();
    inv = inv.replaceAll(" ", "");
    inv = inv.replaceAll(",", "','");
    inv = inv.substring(1, inv.length() - 1);
    inv = "'" + inv + "'";
    String insertSQL = "insert into " + tableName + " (" + inf + ") values (" + inv + ");\n\n";

    insertSqlArea.setText(selectSQL + updateSQL + insertSQL);
}

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

private void manualDefineDbFieldBtnAction() {
    String manualDefineText = manualDefineArea.getText();
    String manualDefinePkText = StringUtils.defaultString(manualDefinePkArea.getText());
    if (StringUtils.isBlank(manualDefineText)) {
        JCommonUtil._jOptionPane_showMessageDialog_error("?");
        return;//from  ww w. j a v a2  s.  com
    }

    Set<String> useList = new HashSet<String>();
    StringTokenizer tok = new StringTokenizer(manualDefineText);
    while (tok.hasMoreElements()) {
        String f = (String) tok.nextElement();
        useList.add(f);
    }

    Set<String> pkList = new HashSet<String>();
    StringTokenizer tok2 = new StringTokenizer(manualDefinePkText);
    while (tok2.hasMoreElements()) {
        String f = (String) tok2.nextElement();
        pkList.add(f);
    }

    Map<String, String> pkMap = new HashMap<String, String>();
    Map<String, String> columnMap = new HashMap<String, String>();
    DefaultListModel dbFieldListModel = (DefaultListModel) dbFieldList.getModel();
    if (dbFieldListModel.isEmpty()) {
        JCommonUtil._jOptionPane_showMessageDialog_error("\"?ObnfString\"");
        return;
    }
    for (Enumeration<?> enu = dbFieldListModel.elements(); enu.hasMoreElements();) {
        KeyValue kv = (KeyValue) enu.nextElement();
        if (!pkList.isEmpty() && pkList.contains(kv.getKey())) {
            pkMap.put(kv.getKey(), kv.value);
        } else if (pkList.isEmpty() && kv.pk) {
            pkMap.put(kv.getKey(), kv.value);
        } else {
            columnMap.put(kv.getKey(), kv.value);
        }
    }

    StringBuffer sb = new StringBuffer();
    sb.append("COLUMN=>\n");
    this.keepKey(columnMap, useList, sb);
    JCommonUtil._jOptionPane_showMessageDialog_info(sb);

    this.putToDbFieldList(pkMap, columnMap);
}