Example usage for javax.swing DefaultListModel getElementAt

List of usage examples for javax.swing DefaultListModel getElementAt

Introduction

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

Prototype

public E getElementAt(int index) 

Source Link

Document

Returns the component at the specified index.

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  2 s  . c  om
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 5; j++) {
            model.addElement(labels[j]);
        }
    }
    System.out.println(model.getElementAt(1));

    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[] args) {
    final DefaultListModel<String> model = new DefaultListModel<>();
    final JList<String> list = new JList<>(model);
    JFrame f = new JFrame();

    model.addElement("A");
    model.addElement("B");
    model.addElement("C");
    model.addElement("D");
    model.addElement("E");

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

    JPanel leftPanel = new JPanel();
    JPanel rightPanel = new JPanel();

    leftPanel.setLayout(new BorderLayout());
    rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));

    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

    list.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                int index = list.locationToIndex(e.getPoint());
                Object item = model.getElementAt(index);
                String text = JOptionPane.showInputDialog("Rename item", item);
                String newitem = "";
                if (text != null)
                    newitem = text.trim();
                else
                    return;

                if (!newitem.isEmpty()) {
                    model.remove(index);
                    model.add(index, newitem);
                    ListSelectionModel selmodel = list.getSelectionModel();
                    selmodel.setLeadSelectionIndex(index);
                }/*from  w  w w.j  a  v a  2  s  . com*/
            }
        }
    });
    leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    leftPanel.add(new JScrollPane(list));

    JButton removeall = new JButton("Remove All");
    JButton add = new JButton("Add");
    JButton rename = new JButton("Rename");
    JButton delete = new JButton("Delete");

    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = JOptionPane.showInputDialog("Add a new item");
            String item = null;

            if (text != null)
                item = text.trim();
            else
                return;

            if (!item.isEmpty())
                model.addElement(item);
        }
    });

    delete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            ListSelectionModel selmodel = list.getSelectionModel();
            int index = selmodel.getMinSelectionIndex();
            if (index >= 0)
                model.remove(index);
        }

    });

    rename.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ListSelectionModel selmodel = list.getSelectionModel();
            int index = selmodel.getMinSelectionIndex();
            if (index == -1)
                return;
            Object item = model.getElementAt(index);
            String text = JOptionPane.showInputDialog("Rename item", item);
            String newitem = null;

            if (text != null) {
                newitem = text.trim();
            } else
                return;

            if (!newitem.isEmpty()) {
                model.remove(index);
                model.add(index, newitem);
            }
        }
    });

    removeall.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.clear();
        }
    });

    rightPanel.add(add);
    rightPanel.add(rename);
    rightPanel.add(delete);
    rightPanel.add(removeall);

    rightPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));

    panel.add(leftPanel);
    panel.add(rightPanel);

    f.add(panel);

    f.setSize(350, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

}

From source file:List.java

public static void main(String[] args) {
    final DefaultListModel model = new DefaultListModel();
    final JList list = new JList(model);
    JFrame f = new JFrame();
    f.setTitle("JList models");

    model.addElement("A");
    model.addElement("B");
    model.addElement("C");
    model.addElement("D");
    model.addElement("E");

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

    JPanel leftPanel = new JPanel();
    JPanel rightPanel = new JPanel();

    leftPanel.setLayout(new BorderLayout());
    rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));

    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

    list.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                int index = list.locationToIndex(e.getPoint());
                Object item = model.getElementAt(index);
                String text = JOptionPane.showInputDialog("Rename item", item);
                String newitem = "";
                if (text != null)
                    newitem = text.trim();
                else
                    return;

                if (!newitem.isEmpty()) {
                    model.remove(index);
                    model.add(index, newitem);
                    ListSelectionModel selmodel = list.getSelectionModel();
                    selmodel.setLeadSelectionIndex(index);
                }//from  ww  w . j  av a 2s. c o m
            }
        }
    });
    leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    leftPanel.add(new JScrollPane(list));

    JButton removeall = new JButton("Remove All");
    JButton add = new JButton("Add");
    JButton rename = new JButton("Rename");
    JButton delete = new JButton("Delete");

    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = JOptionPane.showInputDialog("Add a new item");
            String item = null;

            if (text != null)
                item = text.trim();
            else
                return;

            if (!item.isEmpty())
                model.addElement(item);
        }
    });

    delete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            ListSelectionModel selmodel = list.getSelectionModel();
            int index = selmodel.getMinSelectionIndex();
            if (index >= 0)
                model.remove(index);
        }

    });

    rename.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ListSelectionModel selmodel = list.getSelectionModel();
            int index = selmodel.getMinSelectionIndex();
            if (index == -1)
                return;
            Object item = model.getElementAt(index);
            String text = JOptionPane.showInputDialog("Rename item", item);
            String newitem = null;

            if (text != null) {
                newitem = text.trim();
            } else
                return;

            if (!newitem.isEmpty()) {
                model.remove(index);
                model.add(index, newitem);
            }
        }
    });

    removeall.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.clear();
        }
    });

    rightPanel.add(add);
    rightPanel.add(rename);
    rightPanel.add(delete);
    rightPanel.add(removeall);

    rightPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));

    panel.add(leftPanel);
    panel.add(rightPanel);

    f.add(panel);

    f.setSize(350, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

}

From source file:Main.java

public void contentsChanged(ListDataEvent evt) {
    DefaultListModel model = (DefaultListModel) evt.getSource();
    int start = evt.getIndex0();
    int end = evt.getIndex1();
    int count = end - start + 1;
    for (int i = start; i <= end; i++) {
        Object item = model.getElementAt(i);
    }/* w w  w . j a  va2s.  c  om*/
}

From source file:Main.java

public void intervalAdded(ListDataEvent evt) {
    DefaultListModel model = (DefaultListModel) evt.getSource();
    int start = evt.getIndex0();
    int end = evt.getIndex1();
    int count = end - start + 1;

    for (int i = start; i <= end; i++) {
        Object item = model.getElementAt(i);
    }/*from  w  w w.  j  a  v  a 2  s. c  o  m*/
}

From source file:fr.duminy.jbackup.swing.SourceListTypeMapper.java

@Nullable
@Override//from   ww  w  . j a v  a2  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));
    }//from w w w . j ava 2  s  .  com
    if (sb.length() != 0) {
        sb.deleteCharAt(sb.length() - 1);
    }
    jTextArea1.setText(sb.toString());
}

From source file:com.iisigroup.ris.WebFileScanUtilBrowserUI.java

void setOpenFileListToolTip() {
    int pos = openFileList.getLeadSelectionIndex();
    if (pos == -1) {
        return;//from   ww  w . jav a 2  s . c  om
    }
    DefaultListModel model = (DefaultListModel) openFileList.getModel();
    MFile file = (MFile) model.getElementAt(pos);
    openFileList.setToolTipText(file.file.getAbsolutePath());
}

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

private void executeBtnActionPerformed(ActionEvent evt) {
    try {/*from  w w w .j a v a  2 s . com*/
        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);
    }
}

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

private void initGUI() {
    try {/*from   www . j a  va 2  s  .c o  m*/
        BorderLayout thisLayout = new BorderLayout();
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(thisLayout);
        setTitle("??importjava");
        {
            jTabbedPane1 = new JTabbedPane();
            getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
            {
                jPanel1 = new JPanel();
                BorderLayout jPanel1Layout = new BorderLayout();
                jPanel1.setLayout(jPanel1Layout);
                jTabbedPane1.addTab("", null, jPanel1, null);
                {
                    classPathText = new JTextField();
                    jPanel1.add(classPathText, BorderLayout.NORTH);
                    classPathText.addMouseListener(new MouseAdapter() {
                        public void mouseClicked(MouseEvent evt) {
                            if (StringUtils.isNotBlank(classPathText.getText())) {
                                String text = classPathText.getText();
                                DefaultListModel model = (DefaultListModel) classPathList.getModel();
                                boolean findOk = false;
                                for (int ii = 0; ii < model.size(); ii++) {
                                    String val = (String) model.getElementAt(ii);
                                    if (StringUtils.equals(val, text)) {
                                        findOk = true;
                                    }
                                }
                                if (!findOk) {
                                    model.addElement(text);
                                }
                            }
                        }
                    });
                }
                {
                    jScrollPane1 = new JScrollPane();
                    jPanel1.add(jScrollPane1, BorderLayout.CENTER);
                    jScrollPane1.setPreferredSize(new java.awt.Dimension(492, 302));
                    {
                        DefaultListModel classPathListModel = new DefaultListModel();
                        classPathList = new JList();

                        Set<String> clzSet = new HashSet<String>();
                        for (Enumeration<?> enu = configBean.getConfigProp().keys(); enu.hasMoreElements();) {
                            String key = (String) enu.nextElement();
                            if (key.contains(CLASSNAME_KEY)) {
                                clzSet.add(configBean.getConfigProp().getProperty(key));
                            }
                        }
                        for (String clzName : clzSet) {
                            classPathListModel.addElement(clzName);
                        }

                        jScrollPane1.setViewportView(classPathList);
                        classPathList.setModel(classPathListModel);
                        classPathList.addKeyListener(new KeyAdapter() {
                            public void keyPressed(KeyEvent evt) {
                                JListUtil.newInstance(classPathList).defaultJListKeyPressed(evt);
                            }
                        });
                    }
                }
                {
                    executeBtn = new JButton();
                    jPanel1.add(executeBtn, BorderLayout.WEST);
                    executeBtn.setText("\u57f7\u884c");
                    executeBtn.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            executeBtnActionPerformed(evt);
                        }
                    });
                }
                {
                    srcPathText = new JTextField();
                    if (configBean.getConfigProp().containsKey(SRCPATHTEXT_KEY)) {
                        srcPathText.setText(configBean.getConfigProp().getProperty(SRCPATHTEXT_KEY));
                    }

                    jPanel1.add(srcPathText, BorderLayout.SOUTH);
                    JCommonUtil.jTextFieldSetFilePathMouseEvent(srcPathText, true);
                }
            }
            {
                jPanel2 = new JPanel();
                BorderLayout jPanel2Layout = new BorderLayout();
                jPanel2.setLayout(jPanel2Layout);
                jTabbedPane1.addTab("log", null, jPanel2, null);
                {
                    jScrollPane2 = new JScrollPane();
                    jPanel2.add(jScrollPane2, BorderLayout.CENTER);
                    jScrollPane2.setPreferredSize(new java.awt.Dimension(530, 350));
                    {
                        logArea = new JTextArea();
                        jScrollPane2.setViewportView(logArea);
                    }
                }
            }
        }
        pack();
        this.setSize(551, 417);
    } catch (Exception e) {
        //add your error handling code here
        e.printStackTrace();
    }
}