Example usage for javax.swing DefaultListModel setElementAt

List of usage examples for javax.swing DefaultListModel setElementAt

Introduction

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

Prototype

public void setElementAt(E element, int index) 

Source Link

Document

Sets the component at the specified index of this list to be the specified element.

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);//from ww w  . j  av  a2 s.c o m
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 5; j++) {
            model.addElement(labels[j]);
        }
    }
    model.setElementAt("0", 0);

    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: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]);/*from  www  .  j  a  va  2 s . 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]);/*from  w ww.  j  av a  2  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 www  .j a  v  a  2s .  co  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:gtu._work.ui.RegexDirReplacer.java

private void initGUI() {
    try {/*w  w  w .ja  v  a2s .c  o  m*/
        {
        }
        BorderLayout thisLayout = new BorderLayout();
        getContentPane().setLayout(thisLayout);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        {
            jTabbedPane1 = new JTabbedPane();
            getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
            {
                jPanel1 = new JPanel();
                BorderLayout jPanel1Layout = new BorderLayout();
                jPanel1.setLayout(jPanel1Layout);
                jTabbedPane1.addTab("source", null, jPanel1, null);
                {
                    jScrollPane1 = new JScrollPane();
                    jPanel1.add(jScrollPane1, BorderLayout.CENTER);
                    {
                        ListModel srcListModel = new DefaultListModel();
                        srcList = new JList();
                        jScrollPane1.setViewportView(srcList);
                        srcList.setModel(srcListModel);
                        {
                            panel = new JPanel();
                            jScrollPane1.setRowHeaderView(panel);
                            panel.setLayout(new FormLayout(
                                    new ColumnSpec[] { ColumnSpec.decode("default:grow"), },
                                    new RowSpec[] { FormFactory.DEFAULT_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
                                            FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
                                            FormFactory.DEFAULT_ROWSPEC, }));
                            {
                                childrenDirChkbox = new JCheckBox("??");
                                childrenDirChkbox.setSelected(true);
                                panel.add(childrenDirChkbox, "1, 1");
                            }
                            {
                                subFileNameText = new JTextField();
                                panel.add(subFileNameText, "1, 2, fill, default");
                                subFileNameText.setColumns(10);
                                subFileNameText.setText("(txt|java)");
                            }
                            {
                                replaceOldFileChkbox = new JCheckBox("");
                                replaceOldFileChkbox.setSelected(true);
                                panel.add(replaceOldFileChkbox, "1, 3");
                            }
                            {
                                charsetText = new JTextField();
                                panel.add(charsetText, "1, 5, fill, default");
                                charsetText.setColumns(10);
                                charsetText.setText("UTF8");
                            }
                        }
                        srcList.addMouseListener(new MouseAdapter() {
                            public void mouseClicked(MouseEvent evt) {
                                JListUtil.newInstance(srcList).defaultMouseClickOpenFile(evt);

                                JPopupMenuUtil.newInstance(srcList).applyEvent(evt)//
                                        .addJMenuItem("load files from clipboard", new ActionListener() {
                                            public void actionPerformed(ActionEvent arg0) {
                                                String content = ClipboardUtil.getInstance().getContents();
                                                DefaultListModel model = (DefaultListModel) srcList.getModel();
                                                StringTokenizer tok = new StringTokenizer(content, "\t\n\r\f",
                                                        false);
                                                for (; tok.hasMoreElements();) {
                                                    String val = ((String) tok.nextElement()).trim();
                                                    model.addElement(new File(val));
                                                }
                                            }
                                        }).show();
                            }
                        });
                        srcList.addKeyListener(new KeyAdapter() {
                            public void keyPressed(KeyEvent evt) {
                                JListUtil.newInstance(srcList).defaultJListKeyPressed(evt);
                            }
                        });
                    }
                }
                {
                    addDirFiles = new JButton();
                    jPanel1.add(addDirFiles, BorderLayout.NORTH);
                    addDirFiles.setText("add dir files");
                    addDirFiles.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            File file = JFileChooserUtil.newInstance().selectDirectoryOnly().showOpenDialog()
                                    .getApproveSelectedFile();
                            if (file == null || !file.isDirectory()) {
                                return;
                            }

                            List<File> fileLst = new ArrayList<File>();

                            String subName = StringUtils.trimToEmpty(subFileNameText.getText());
                            if (StringUtils.isBlank(subName)) {
                                subName = ".*";
                            }
                            String patternStr = ".*\\." + subName;

                            if (childrenDirChkbox.isSelected()) {
                                FileUtil.searchFileMatchs(file, patternStr, fileLst);
                            } else {
                                for (File f : file.listFiles()) {
                                    if (f.isFile() && f.getName().matches(patternStr)) {
                                        fileLst.add(f);
                                    }
                                }
                            }

                            DefaultListModel model = new DefaultListModel();
                            for (File f : fileLst) {
                                model.addElement(f);
                            }
                            srcList.setModel(model);
                        }
                    });
                }
            }
            {
                jPanel2 = new JPanel();
                BorderLayout jPanel2Layout = new BorderLayout();
                jPanel2.setLayout(jPanel2Layout);
                jTabbedPane1.addTab("param", null, jPanel2, null);
                {
                    exeucte = new JButton();
                    jPanel2.add(exeucte, BorderLayout.SOUTH);
                    exeucte.setText("exeucte");
                    exeucte.setPreferredSize(new java.awt.Dimension(491, 125));
                    exeucte.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            exeucteActionPerformed(evt);
                        }
                    });
                }
                {
                    jPanel3 = new JPanel();
                    GroupLayout jPanel3Layout = new GroupLayout((JComponent) jPanel3);
                    jPanel3.setLayout(jPanel3Layout);
                    jPanel2.add(jPanel3, BorderLayout.CENTER);
                    {
                        repFromText = new JTextField();
                    }
                    {
                        repToText = new JTextField();
                    }
                    jPanel3Layout.setHorizontalGroup(jPanel3Layout.createSequentialGroup()
                            .addContainerGap(25, 25)
                            .addGroup(jPanel3Layout.createParallelGroup()
                                    .addGroup(jPanel3Layout.createSequentialGroup().addComponent(repFromText,
                                            GroupLayout.PREFERRED_SIZE, 446, GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel3Layout.createSequentialGroup().addComponent(repToText,
                                            GroupLayout.PREFERRED_SIZE, 446, GroupLayout.PREFERRED_SIZE)))
                            .addContainerGap(20, Short.MAX_VALUE));
                    jPanel3Layout.setVerticalGroup(jPanel3Layout.createSequentialGroup().addContainerGap()
                            .addComponent(repFromText, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE,
                                    GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(repToText, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
                }
                {
                    addToTemplate = new JButton();
                    jPanel2.add(addToTemplate, BorderLayout.NORTH);
                    addToTemplate.setText("add to template");
                    addToTemplate.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            prop.put(repFromText.getText(), repToText.getText());
                            reloadTemplateList();
                        }
                    });
                }
            }
            {
                jPanel4 = new JPanel();
                BorderLayout jPanel4Layout = new BorderLayout();
                jPanel4.setLayout(jPanel4Layout);
                jTabbedPane1.addTab("result", null, jPanel4, null);
                {
                    jScrollPane2 = new JScrollPane();
                    jPanel4.add(jScrollPane2, BorderLayout.CENTER);
                    {

                        ListModel newRepListModel = new DefaultListModel();
                        newRepList = new JList();
                        jScrollPane2.setViewportView(newRepList);
                        newRepList.setModel(newRepListModel);
                        newRepList.addMouseListener(new MouseAdapter() {
                            static final String tortoiseMergeExe = "TortoiseMerge.exe";
                            static final String commandFormat = "cmd /c call \"%s\" /base:\"%s\" /theirs:\"%s\"";

                            public void mouseClicked(MouseEvent evt) {
                                if (!JListUtil.newInstance(newRepList).isCorrectMouseClick(evt)) {
                                    return;
                                }

                                OldNewFile oldNewFile = (OldNewFile) JListUtil
                                        .getLeadSelectionObject(newRepList);

                                String base = oldNewFile.newFile.getAbsolutePath();
                                String theirs = oldNewFile.oldFile.getAbsolutePath();

                                String command = String.format(commandFormat, tortoiseMergeExe, base, theirs);

                                System.out.println(command);

                                try {
                                    Runtime.getRuntime().exec(command);
                                } catch (IOException e) {
                                    JCommonUtil.handleException(e);
                                }
                            }
                        });
                        newRepList.addKeyListener(new KeyAdapter() {
                            public void keyPressed(KeyEvent evt) {
                                Object[] objects = (Object[]) newRepList.getSelectedValues();
                                if (objects == null || objects.length == 0) {
                                    return;
                                }
                                DefaultListModel model = (DefaultListModel) newRepList.getModel();
                                int lastIndex = model.getSize() - 1;
                                Object swap = null;

                                StringBuilder dsb = new StringBuilder();
                                for (Object current : objects) {
                                    int index = model.indexOf(current);
                                    switch (evt.getKeyCode()) {
                                    case 38:// up
                                        if (index != 0) {
                                            swap = model.getElementAt(index - 1);
                                            model.setElementAt(swap, index);
                                            model.setElementAt(current, index - 1);
                                        }
                                        break;
                                    case 40:// down
                                        if (index != lastIndex) {
                                            swap = model.getElementAt(index + 1);
                                            model.setElementAt(swap, index);
                                            model.setElementAt(current, index + 1);
                                        }
                                        break;
                                    case 127:// del
                                        OldNewFile current_ = (OldNewFile) current;
                                        dsb.append(current_.newFile.getName() + "\t"
                                                + (current_.newFile.delete() ? "T" : "F") + "\n");
                                        current_.newFile.delete();
                                        model.removeElement(current);
                                    }
                                }

                                if (dsb.length() > 0) {
                                    JOptionPaneUtil.newInstance().iconInformationMessage()
                                            .showMessageDialog("del result!\n" + dsb, "DELETE");
                                }
                            }
                        });

                    }
                }
                {
                    replaceOrignFile = new JButton();
                    jPanel4.add(replaceOrignFile, BorderLayout.SOUTH);
                    replaceOrignFile.setText("replace orign file");
                    replaceOrignFile.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            DefaultListModel model = (DefaultListModel) newRepList.getModel();
                            StringBuilder sb = new StringBuilder();
                            for (int ii = 0; ii < model.size(); ii++) {
                                OldNewFile file = (OldNewFile) model.getElementAt(ii);
                                boolean delSuccess = false;
                                boolean renameSuccess = false;
                                if (delSuccess = file.oldFile.delete()) {
                                    renameSuccess = file.newFile.renameTo(file.oldFile);
                                }
                                sb.append(file.oldFile.getName() + " del:" + (delSuccess ? "T" : "F")
                                        + " rename:" + (renameSuccess ? "T" : "F") + "\n");
                            }
                            JOptionPaneUtil.newInstance().iconInformationMessage().showMessageDialog(sb,
                                    getTitle());
                        }
                    });
                }
            }
            {
                jPanel5 = new JPanel();
                BorderLayout jPanel5Layout = new BorderLayout();
                jPanel5.setLayout(jPanel5Layout);
                jTabbedPane1.addTab("template", null, jPanel5, null);
                {
                    jScrollPane3 = new JScrollPane();
                    jPanel5.add(jScrollPane3, BorderLayout.CENTER);
                    {
                        templateList = new JList();
                        reloadTemplateList();
                        jScrollPane3.setViewportView(templateList);
                        templateList.addMouseListener(new MouseAdapter() {
                            public void mouseClicked(MouseEvent evt) {
                                if (templateList.getLeadSelectionIndex() == -1) {
                                    return;
                                }
                                Entry<Object, Object> entry = (Entry<Object, Object>) JListUtil
                                        .getLeadSelectionObject(templateList);
                                repFromText.setText((String) entry.getKey());
                                repToText.setText((String) entry.getValue());
                            }
                        });
                        templateList.addKeyListener(new KeyAdapter() {
                            public void keyPressed(KeyEvent evt) {
                                JListUtil.newInstance(templateList).defaultJListKeyPressed(evt);
                            }
                        });
                    }
                }
                {
                    scheduleExecute = new JButton();
                    jPanel5.add(scheduleExecute, BorderLayout.SOUTH);
                    scheduleExecute.setText("schedule execute");
                    scheduleExecute.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            scheduleExecuteActionPerformed(evt);
                        }
                    });
                }
            }
        }
        this.setSize(512, 350);

        JCommonUtil.setFontAll(this.getRootPane());

        JCommonUtil.frameCloseDo(this, new WindowAdapter() {
            public void windowClosing(WindowEvent paramWindowEvent) {
                if (StringUtils.isNotBlank(repFromText.getText())) {
                    prop.put(repFromText.getText(), repToText.getText());
                }
                try {
                    prop.store(new FileOutputStream(propFile), "regexText");
                } catch (Exception e) {
                    JCommonUtil.handleException("properties store error!", e);
                }
                setVisible(false);
                dispose();
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.yccheok.jstock.gui.IndicatorPanel.java

private void alertIndicatorRename() {
    final String oldProjectName = (String) this.jList1.getSelectedValue();
    if (oldProjectName == null) {
        JOptionPane.showMessageDialog(this,
                MessagesBundle.getString("warning_message_you_must_select_alert_indicator"),
                MessagesBundle.getString("warning_title_you_must_select_alert_indicator"),
                JOptionPane.WARNING_MESSAGE);
        return;//from w  ww . j  av a2s.  c  om
    }
    assert (this.alertIndicatorProjectManager.contains(oldProjectName));

    String newProjectName = null;

    while (true) {
        newProjectName = JOptionPane.showInputDialog(this,
                MessagesBundle.getString("info_message_enter_rename_alert_indicator_name"), oldProjectName);

        if (newProjectName == null) {
            return;
        }

        newProjectName = newProjectName.trim();

        if (newProjectName.length() == 0) {
            JOptionPane.showMessageDialog(this,
                    MessagesBundle.getString("warning_message_you_need_to_specific_alert_indicator_name"),
                    MessagesBundle.getString("warning_title_you_need_to_specific_alert_indicator_name"),
                    JOptionPane.WARNING_MESSAGE);
            continue;
        }

        if (this.alertIndicatorProjectManager.contains(newProjectName)) {
            JOptionPane.showMessageDialog(this,
                    MessagesBundle.getString("warning_message_already_an_alert_indicator_with_same_name"),
                    MessagesBundle.getString("warning_title_already_an_alert_indicator_with_same_name"),
                    JOptionPane.WARNING_MESSAGE);
            continue;
        }

        if (this.alertIndicatorProjectManager.renameProject(newProjectName, oldProjectName)) {
            final DefaultListModel defaultListModel = (DefaultListModel) this.jList1.getModel();
            defaultListModel.setElementAt(newProjectName, this.jList1.getSelectedIndex());
            // sync may not work well for rename operation, as the method
            // may false thought we are deleting oldProjectName, and added
            // newProjectName. We will then perform wrong selection on the
            // project.
            //this.syncJListWithIndicatorProjectManager(this.jList1, this.alertIndicatorProjectManager);

            // Update the project.xml as well.
            IndicatorPanel.this.saveAlertIndicatorProjectManager();

            // Update selection info as well.
            if (this.listSelectionEx != null) {
                this.listSelectionEx = ListSelectionEx.newInstance(this.listSelectionEx.list, newProjectName);
            }
        } else {
            JOptionPane.showMessageDialog(this,
                    MessagesBundle.getString("error_message_unknown_error_during_renaming"),
                    MessagesBundle.getString("error_title_unknown_error_during_renaming"),
                    JOptionPane.ERROR_MESSAGE);
        }

        break;
    }
}

From source file:org.yccheok.jstock.gui.IndicatorPanel.java

private void moduleIndicatorRename() {
    final String oldProjectName = (String) this.jList2.getSelectedValue();
    if (oldProjectName == null) {
        JOptionPane.showMessageDialog(this,
                MessagesBundle.getString("warning_message_you_must_select_module_indicator"),
                MessagesBundle.getString("warning_title_you_must_select_module_indicator"),
                JOptionPane.WARNING_MESSAGE);
        return;/*from   ww  w .j  a v  a  2 s .co m*/
    }
    assert (this.moduleIndicatorProjectManager.contains(oldProjectName));

    String newProjectName = null;

    while (true) {
        newProjectName = JOptionPane.showInputDialog(this,
                MessagesBundle.getString("info_message_enter_rename_module_indicator_name"), oldProjectName);

        if (newProjectName == null) {
            return;
        }

        newProjectName = newProjectName.trim();

        if (newProjectName.length() == 0) {
            JOptionPane.showMessageDialog(this,
                    MessagesBundle.getString("warning_message_you_need_to_specific_module_indicator_name"),
                    MessagesBundle.getString("warning_title_you_need_to_specific_module_indicator_name"),
                    JOptionPane.WARNING_MESSAGE);
            continue;
        }

        if (this.moduleIndicatorProjectManager.contains(newProjectName)) {
            JOptionPane.showMessageDialog(this,
                    MessagesBundle.getString("warning_message_already_a_module_indicator_with_same_name"),
                    MessagesBundle.getString("warning_title_already_a_module_indicator_with_same_name"),
                    JOptionPane.WARNING_MESSAGE);
            continue;
        }

        if (this.moduleIndicatorProjectManager.renameProject(newProjectName, oldProjectName)) {
            final DefaultListModel defaultListModel = (DefaultListModel) this.jList2.getModel();
            defaultListModel.setElementAt(newProjectName, this.jList2.getSelectedIndex());
            // sync may not work well for rename operation, as the method
            // may false thought we are deleting oldProjectName, and added
            // newProjectName. We will then perform wrong selection on the
            // project.
            //this.syncJListWithIndicatorProjectManager(this.jList2, this.moduleIndicatorProjectManager);
            // Update the project.xml as well.
            IndicatorPanel.this.saveModuleIndicatorProjectManager();
        } else {
            JOptionPane.showMessageDialog(this,
                    MessagesBundle.getString("error_message_unknown_error_during_renaming"),
                    MessagesBundle.getString("error_title_unknown_error_during_renaming"),
                    JOptionPane.ERROR_MESSAGE);
        }

        break;
    }
}