简体   繁体   English

更新JList时出现问题

[英]Problem when updating a JList

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. 我有两个JPLnels,其中包含两个JList,一个用于单词列表,另一个用于对应的同义词。 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 单击一个JList上的单词时,对应的同义词出现在另一个JList上,然后有2个JTextField,用于添加单词同义词对,在一个字段中插入单词,在另一个字段中插入同义词,拆分它们创建一个包含synonyms.split(“”)的数组并将其存储在Map中这是我添加到单词JList的ListSelectionListener

    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. 如果我在没有选择项目的情况下在列表中添加单词和同义词它可以正常工作,如果我执行添加项目的操作,而我在word-JList中选择了一个单词,方法getValues()使用空字符串被称为,抛出异常。 i dont understand why, adding an element on the list also fires a list selection. 我不明白为什么,在列表中添加元素也会触发列表选择。 Any advice? 有什么建议?

我建议使用DefaultListModel来操作JList的内容。

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 . 下载并测试如何使用列表的ListDemo示例。 When you run the code and "Hire" a person, then the list selection event fires (I added a System.out.println(...) to the listener). 当您运行代码并“雇用”一个人时,列表选择事件将触发(我向侦听器添加了一个System.out.println(...))。 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. 如果您需要更多帮助,请发布一个证明问题的SSCCE

This is a SSCCE of my project, i've left out all exceptions and comments and non relevant buttons, labels, methods. 这是我项目的SSCCE,我遗漏了所有例外和评论以及不相关的按钮,标签,方法。 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.AbstractListModel; import javax.swing.DefaultListModel; import javax.swing.DefaultListModel; import javax.swing.JList; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JScrollPane; import java.awt.Rectangle; import java.awt.Rectangle; import java.util.HashMap; import java.util.HashMap; import java.util.Map; import java.util.Map; import java.util.Set; import java.util.Set; import javax.swing.JTextField; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JButton; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionListener;

public class Synonims extends JFrame { 公共类Synonims扩展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);

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM