Example usage for javax.swing DefaultListModel getSize

List of usage examples for javax.swing DefaultListModel getSize

Introduction

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

Prototype

public int getSize() 

Source Link

Document

Returns the number of components 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);

    // Remove the first item
    int pos = 0;//w w w .  j a  va  2  s .  com
    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(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);/*from   w ww  .j a  va  2s  . c  o m*/
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 5; j++) {
            model.addElement(labels[j]);
        }
    }
    System.out.println(model.getSize());

    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: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 w  w . j av a 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: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  ww.j  av  a2s. c  o m
    }
    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: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 a2 s  .c o m
    }
    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]);//from  w w  w  .  j  av  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:fr.duminy.jbackup.swing.SourceListTypeMapper.java

@Nullable
@Override//from   w w  w.ja  va 2  s.c o m
public List<Source> getValue(@Nonnull ListPanel<Source, JList<Source>> listPanel) {
    List<Source> result = new ArrayList<>();

    DefaultListModel<Source> model = getModel(listPanel);
    for (int i = 0; i < model.getSize(); i++) {
        result.add(model.getElementAt(i));
    }

    return result;
}

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

private void jButton2ActionPerformed(ActionEvent evt) {
    DefaultListModel model = (DefaultListModel) jList1.getModel();
    StringBuilder sb = new StringBuilder();
    for (int ii = 0; ii < model.getSize(); ii++) {
        String val = (String) model.getElementAt(ii);
        sb.append(String.format("\"%s\",", val));
    }/* ww  w  .  j a v  a  2 s  .  c o m*/
    if (sb.length() != 0) {
        sb.deleteCharAt(sb.length() - 1);
    }
    jTextArea1.setText(sb.toString());
}

From source file:DragListDemo.java

public boolean importData(JComponent c, Transferable t) {
    JList target = null;//from www .j a va  2  s  .  c  o  m
    ArrayList alist = null;
    if (!canImport(c, t.getTransferDataFlavors())) {
        return false;
    }
    try {
        target = (JList) c;
        if (hasLocalArrayListFlavor(t.getTransferDataFlavors())) {
            alist = (ArrayList) t.getTransferData(localArrayListFlavor);
        } else if (hasSerialArrayListFlavor(t.getTransferDataFlavors())) {
            alist = (ArrayList) t.getTransferData(serialArrayListFlavor);
        } else {
            return false;
        }
    } catch (UnsupportedFlavorException ufe) {
        System.out.println("importData: unsupported data flavor");
        return false;
    } catch (IOException ioe) {
        System.out.println("importData: I/O exception");
        return false;
    }

    //At this point we use the same code to retrieve the data
    //locally or serially.

    //We'll drop at the current selected index.
    int index = target.getSelectedIndex();

    //Prevent the user from dropping data back on itself.
    //For example, if the user is moving items #4,#5,#6 and #7 and
    //attempts to insert the items after item #5, this would
    //be problematic when removing the original items.
    //This is interpreted as dropping the same data on itself
    //and has no effect.
    if (source.equals(target)) {
        if (indices != null && index >= indices[0] - 1 && index <= indices[indices.length - 1]) {
            indices = null;
            return true;
        }
    }

    DefaultListModel listModel = (DefaultListModel) target.getModel();
    int max = listModel.getSize();
    if (index < 0) {
        index = max;
    } else {
        index++;
        if (index > max) {
            index = max;
        }
    }
    addIndex = index;
    addCount = alist.size();
    for (int i = 0; i < alist.size(); i++) {
        listModel.add(index++, alist.get(i));
    }
    return true;
}

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

private void executeBtnActionPerformed(ActionEvent evt) {
    try {/* ww  w .  j  a  va 2 s. c o m*/
        Validate.notBlank(srcPathText.getText(), "java source??");
        Validate.isTrue(classPathList.getModel().getSize() != 0, "classPathList??");

        logArea.setText("");
        PrintStream out = JCommonUtil.getNewPrintStream2JTextArea(logArea, 0, false);

        Pattern importPattern = Pattern.compile("import\\s+([\\w\\.]+)\\;");
        Pattern classNamePattern = Pattern.compile("(class|interface)\\s+\\w+[\\w\\s]*\\{");

        Set<String> classNameSet = new HashSet<String>();
        DefaultListModel model = (DefaultListModel) classPathList.getModel();
        for (int ii = 0; ii < model.getSize(); ii++) {
            String className = (String) model.getElementAt(ii);
            classNameSet.add(className);
        }

        File srcFile = JCommonUtil.filePathCheck(srcPathText.getText(), "java source", true);

        List<File> fileList = new ArrayList<File>();
        FileUtil.searchFilefind(srcFile, ".*\\.[jJ][aA][vV][aA]", fileList);

        Multimap<File, String> javaContainMap = ArrayListMultimap.create();

        Matcher matcher = null;
        for (File file : fileList) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(new FileInputStream(file), "utf8"));
            for (String line = null; (line = reader.readLine()) != null;) {
                if (classNamePattern.matcher(line).find()) {
                    break;
                }

                matcher = importPattern.matcher(line);
                if (matcher.find()) {
                    String importClass = matcher.group(1);
                    for (String ptn : classNameSet) {
                        Pattern pt2 = null;
                        try {
                            pt2 = Pattern.compile(ptn);
                        } catch (Exception ex) {
                        }
                        if (StringUtils.equals(importClass, ptn)) {
                            javaContainMap.put(file, importClass);
                        } else if (importClass.contains(ptn)) {
                            javaContainMap.put(file, importClass);
                        } else if (pt2.matcher(importClass).find()) {
                            javaContainMap.put(file, importClass);
                        }
                    }
                }
            }
            reader.close();
        }

        if (javaContainMap.isEmpty()) {
            out.println("java?importClass");
        } else {
            out.println("?importClass");
            for (File errorFile : javaContainMap.keySet()) {
                out.println(errorFile);
                for (String clzName : javaContainMap.get(errorFile)) {
                    out.println("\t" + clzName);
                }
            }
        }
        out.println("java : " + fileList.size());

        configBean.getConfigProp().put(SRCPATHTEXT_KEY, srcFile.getAbsolutePath());
        for (String clzName : classNameSet) {
            if (configBean.getConfigProp().containsValue(clzName)) {
                continue;
            }
            int availableIndex = -1;
            for (int ii = 0;; ii++) {
                if (!configBean.getConfigProp().containsKey(CLASSNAME_KEY + ii)) {
                    availableIndex = ii;
                    break;
                }
            }
            configBean.getConfigProp().put(CLASSNAME_KEY + availableIndex, clzName);
        }
        configBean.store();

        JCommonUtil._jOptionPane_showMessageDialog_info("???!");

        //            C:/workspace/workspace_farEastStone/estore/fet_estore_search_engie_revamp/revamp_source_code
    } catch (Exception ex) {
        JCommonUtil.handleException(ex);
    }
}