Example usage for javax.swing ListModel getElementAt

List of usage examples for javax.swing ListModel getElementAt

Introduction

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

Prototype

E getElementAt(int index);

Source Link

Document

Returns the value at the specified index.

Usage

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

private void syncJListWithIndicatorProjectManager(JList jList,
        IndicatorProjectManager indicatorProjectManager) {
    final String projectName = (String) jList.getSelectedValue();
    boolean isProjectNameBeingRemoved = false;
    int newSelection = -1;

    final ListModel listModel = jList.getModel();
    for (int i = 0; i < listModel.getSize(); i++) {
        if (indicatorProjectManager.contains(listModel.getElementAt(i).toString()) == false) {
            // Remove from JList, as it is not found in indicator project manager.
            Object removedObject = ((DefaultListModel) listModel).remove(i);
            if (projectName.equals(removedObject)) {
                isProjectNameBeingRemoved = true;
                newSelection = i;/*from w  w  w  . jav  a 2s .com*/
            }
            i--;
        }
    }
    for (int i = 0; i < indicatorProjectManager.getNumOfProject(); i++) {
        final String p = indicatorProjectManager.getProject(i);
        if (((DefaultListModel) listModel).contains(p) == false) {
            // Add to JList, as it is found in indicator project manager.
            ((DefaultListModel) listModel).addElement(p);
        }
    }

    if (!isProjectNameBeingRemoved) {
        // Ensure list cell renderer is being triggered.
        jList.setSelectedValue(projectName, true);
    } else {
        if (newSelection >= jList.getModel().getSize()) {
            // Select last row.
            jList.setSelectedIndex(jList.getModel().getSize() - 1);
        } else {
            jList.setSelectedIndex(newSelection);
        }
    }
}

From source file:unimelb.distributed_project.gui.JacardSimilarityMeasurePanel.java

/**
 * Measure button action perform function for triggering the thread to perform Jacard
 * Similarity measurement. It takes topNearestWordtextField's text as an input parameter to
 * execute the distance measurement in word2vec library.
 *
 * @param e ActionEvent object//  w  ww  .jav  a  2  s. c  om
 */
private void measureButtonActionPerformed(ActionEvent e) {

    if (!word2vecFilePath1.equals("") && word2vecFilePath1 != null && !word2vecFilePath2.equals("")
            && word2vecFilePath2 != null && wordList != null) {
        word2vecFilePath1 = word2vecFilePath1TextField.getText();
        word2vecFilePath2 = word2vecFilePath2TextField.getText();
        jcardSimFilePath = simOutputPathTextField.getText();
        if (!topNearestWordtextField.getText().equals(""))
            topNearestWords = new Integer(topNearestWordtextField.getText());
        Thread measureThread = new Thread(new Runnable() {
            @Override
            public void run() {
                ListModel model = wordList.getModel();

                try {
                    if (jcardSimFilePath.equals(""))
                        jcardSimFilePath = "./jcard_sim.txt";
                    BufferedWriter bw = new BufferedWriter(new FileWriter(jcardSimFilePath));
                    for (int i = 0; i < model.getSize(); i++) {
                        StringBuilder output = new StringBuilder();
                        output.append(model.getElementAt(i) + ",");
                        log.debug(model.getElementAt(i));
                        double jcardSimScore = TweetWord2VecUtils.jcardSim(
                                TweetWord2VecUtils.runModelNearest(word2vecFilePath1,
                                        (String) model.getElementAt(i), topNearestWords),
                                TweetWord2VecUtils.runModelNearest(word2vecFilePath2,
                                        (String) model.getElementAt(i), topNearestWords));
                        output.append(jcardSimScore + "\n");
                        bw.write(output.toString());
                        log.debug(i + "." + jcardSimScore);

                    }

                    bw.close();
                } catch (IOException ioe) {
                    log.debug("create buffer writer fails");

                }

                log.debug(
                        "done JcardSimilarity Measure, enable all the button                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              ");
                browseButton1.setEnabled(true);
                browseButton2.setEnabled(true);
                loadListButton.setEnabled(true);
                measureButton.setEnabled(true);
                browseButton3.setEnabled(true);
                visualizedButton.setEnabled(true);
                JOptionPane.showMessageDialog(mainFrame, "JacardSimilarity Measurement is done",
                        "JacardSimilarity Measurement", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        measureThread.start();
        browseButton1.setEnabled(false);
        browseButton2.setEnabled(false);
        loadListButton.setEnabled(false);
        measureButton.setEnabled(false);
        browseButton3.setEnabled(false);
        visualizedButton.setEnabled(false);
    } else {
        JOptionPane.showMessageDialog(this.mainFrame, "Please suggest files and load wordOfList",
                "file is not selected", JOptionPane.ERROR_MESSAGE);
    }

}