0
votes

I'm having a poblem with my project, basically its a small app that stores synonyms. I have 2 JPanels populated with two JLists, one for the list of words and one with the correspondent synonyms. When you click on a word on one JList, the correspondent synonyms appears on the other JList then there are 2 JTextFields, for adding a word-synonym pair, in one field you insert the word in the other you insert the synonyms, split them creating an array with synonyms.split(" ") and store all in a Map this is the ListSelectionListener i have added to the word JList

    woordenList.addListSelectionListener(new ListSelectionListener() {

              public void valueChanged(ListSelectionEvent evt) {

                if (evt.getValueIsAdjusting()){

                  return;
                }

                getSynoniemen(evt);
             }
            });

And this is the method that gets the synonyms

    private void getSynoniemen(ListSelectionEvent e){

    if(e.getValueIsAdjusting()){


        return;

     }else{


         String woord=(String)woordenList.getSelectedValue();

         Object[] synArray=(Object[])beheer.getValues(woord);

         synoniemenList.setListData(synArray);

     }

}

If i add a word and synonyms into the lists when no item is selected it works fine, if i do this operation of adding items while instead, i have a word selected in the word-JList, the method getValues() with a null string is called, throwing an exception. i dont understand why, adding an element on the list also fires a list selection. Any advice?

3
e.getValueIsAdjusting() in getSynoniemen(...) is redundant. - mre
I'd also recommend reading How to Use Lists, perhaps that will give you some insight. From the code snippet that you've posted, it's difficult to assess the true underlying problem here. - mre

3 Answers

1
votes

i dont understand why, adding an element on the list also fires a list selection.

Somewhere in your code you must be changing the selected index.

Download and test the ListDemo example for How to Use Lists. When you run the code and "Hire" a person, then the list selection event fires (I added a System.out.println(...) to the listener). Then if you comment out:

//            list.setSelectedIndex(index);

the event is not fired. So you have a problem in your code. Compare your code to the working example code to see what if different.

If you need more help then post a SSCCE that demonstrates the problem.

1
votes

I would recommend using the DefaultListModel to manipulate the contents of the JList.

0
votes

This is a SSCCE of my project, i've left out all exceptions and comments and non relevant buttons, labels, methods. Try to insert a word with its synonyms, and then select a word, then while the word is selected try to insert another pair word-synonyms, there will come the exception
import javax.swing.AbstractListModel; import javax.swing.DefaultListModel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JScrollPane; import java.awt.Rectangle; import java.util.HashMap; import java.util.Map; import java.util.Set; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener;

public class Synonims extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JScrollPane wordsPane = null;
private JScrollPane synosymsPane = null;
private JTextField wordField = null;
private JTextField synonymsField = null;
private JButton addButton = null;
private JList wordsList=new JList(new DefaultListModel());
private JList synonymsList=new JList(new DefaultListModel());
private SynonymsManager manager=new SynonymsManager();

//default const
public Synonims() {
    super();
    initialize();
}
//init
private void initialize() {
    this.setSize(413, 285);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setContentPane(getJContentPane());
    this.setTitle("JFrame");
}

//contentpane
private JPanel getJContentPane() {
    if (jContentPane == null) {
        jContentPane = new JPanel();
        jContentPane.setLayout(null);
        jContentPane.add(getWordsPane(), null);
        jContentPane.add(getSynosymsPane(), null);
        jContentPane.add(getWordField(), null);
        jContentPane.add(getSynonymsField(), null);
        jContentPane.add(getAddButton(), null);
    }
    return jContentPane;
}
//RELEVANT: LISTSELECTIONLISTENER
private JScrollPane getWordsPane() {
    if (wordsPane == null) {
        wordsPane = new JScrollPane();
        wordsPane.setBounds(new Rectangle(16, 15, 121, 177));
        wordsPane.getViewport().add(wordsList,null);
        wordsList.addListSelectionListener(new ListSelectionListener() {

              public void valueChanged(ListSelectionEvent evt) {


                getSyn(evt);
             }
            });
    }
    return wordsPane;
}
//RELEVANT: GET THE SYNONYMS OF THE SELECTED WORD
public void getSyn(ListSelectionEvent e){
    String word=(String)wordsList.getSelectedValue();
    Object[] synArray=(Object[])manager.getValues(word);
    synonymsList.setListData(synArray);
}

//synonymspane
private JScrollPane getSynosymsPane() {
    if (synosymsPane == null) {
        synosymsPane = new JScrollPane();
        synosymsPane.setBounds(new Rectangle(254, 14, 133, 181));
        synosymsPane.getViewport().add(synonymsList,null);
    }
    return synosymsPane;
}

//wordfield
private JTextField getWordField() {
    if (wordField == null) {
        wordField = new JTextField();
        wordField.setBounds(new Rectangle(16, 209, 129, 27));
    }
    return wordField;
}

//synonymsfield
private JTextField getSynonymsField() {
    if (synonymsField == null) {
        synonymsField = new JTextField();
        synonymsField.setBounds(new Rectangle(254, 207, 136, 30));
    }
    return synonymsField;
}

//the button
private JButton getAddButton() {
    if (addButton == null) {
        addButton = new JButton();
        addButton.setBounds(new Rectangle(164, 228, 72, 25));
        addButton.setText("save");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                addAction(); 
            }
        });
    }
    return addButton;
}
//RELEVANT: THE METHODS THAT LOADS BOTH WORD AND SYNONYMS
public void addAction(){
String newWord=wordField.getText();
String newSynonyms= synonymsField.getText();
String[] synonymsArray=newSynonyms.split(" ");
manager.addSyn(newWord, synonymsArray);
wordField.setText("");
synonymsField.setText("");
wordsList.setListData(manager.getKeys());
}

//INNER CLASS FOR THE SYNONYMS
public class Synonym{

    private String[] synonym=null;

    public Synonym(String[]synonym){
        this.synonym=synonym;
    }

    public String[] getSynonyms(){
        return this.synonym;
    }
}

//INNER CLASS THAT MANAGES THE SYNONYMS
public class SynonymsManager extends AbstractListModel{
    private Map<String,Synonym> manager=null;

    public SynonymsManager(){
        manager=new HashMap<String,Synonym>();
    }

    public void addSyn(String woord, String[] synonyms){
       Synonym syn=new Synonym(synonyms);
         manager.put(woord, syn);
         fireContentsChanged(this,0,getSize() -1);
    }

    public Object getElementAt(int arg0) {
        return null;
    }
    public int getSize() {
        return 0;
    }

    public Object[] getKeys(){

        Set<String>keySet=manager.keySet();
        Object[] keys=keySet.toArray();
        return keys;
    }
    public Object[] getValues(String woord){

        Object[] values=search(woord).getSynonyms();

        return values;
    }
     public Synonym search(String word){

         return manager.get(word);
     }
}

public static void main(String[] args){

    Synonims s=new Synonims();
    s.setVisible(true);

}

}