更新 JList 时出现问题

发布于 2024-11-16 06:09:52 字数 1097 浏览 1 评论 0原文

我的项目遇到了问题,基本上它是一个存储同义词的小应用程序。 我有 2 个 JPanel,其中填充了两个 JList,一个用于单词列表,另一个用于对应的同义词。 当您单击一个 JList 上的某个单词时,对应的同义词会出现在另一个 JList 上 然后有 2 个 JTextFields,用于添加单词-同义词对,在一个字段中插入单词,在另一个字段中插入同义词,将它们拆分,使用 synonym.split(" ") 创建一个数组,并将所有内容存储在 Map 中 这是我添加到单词 JList 的 ListSelectionListener

    woordenList.addListSelectionListener(new ListSelectionListener() {

              public void valueChanged(ListSelectionEvent evt) {

                if (evt.getValueIsAdjusting()){

                  return;
                }

                getSynoniemen(evt);
             }
            });

这是获取同义词的方法

    private void getSynoniemen(ListSelectionEvent e){

    if(e.getValueIsAdjusting()){


        return;

     }else{


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

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

         synoniemenList.setListData(synArray);

     }

}

如果我在没有选择任何项目时将单词和同义词添加到列表中,它可以正常工作,如果我执行添加项目的操作,而相反,我在 word-JList 中选择了一个单词,调用带有空字符串的方法 getValues() ,引发异常。我不明白为什么,在列表中添加元素也会触发列表选择。 有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉月流沐 2024-11-23 06:09:52

我不明白为什么,在列表上添加元素也会触发列表选择。

您必须在代码中的某个位置更改所选索引。

下载并测试 如何使用列表的 ListDemo 示例。当您运行代码并“雇用”一个人时,就会触发列表选择事件(我向侦听器添加了 System.out.println(...) )。然后,如果您注释掉:

//            list.setSelectedIndex(index);

该事件不会被触发。所以你的代码有问题。将您的代码与工作示例代码进行比较,看看是否不同。

如果您需要更多帮助,请发布一个 SSCCE 来说明问题。

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.

愛放△進行李 2024-11-23 06:09:52

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

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

毅然前行 2024-11-23 06:09:52

这是我的项目的 SSCCE,我省略了所有例外和注释以及不相关的按钮、标签、方法。
尝试插入一个单词及其同义词,然后选择一个单词,然后在选择该单词的同时尝试插入另一对单词同义词,会出现异常
导入 javax.swing.AbstractListModel;
导入 javax.swing.DefaultListModel;
导入 javax.swing.JList;
导入javax.swing.JPanel;
导入 javax.swing.JFrame;
导入 javax.swing.JScrollPane;
导入java.awt.矩形;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Set;
导入 javax.swing.JTextField;
导入 javax.swing.JButton;
导入 javax.swing.event.ListSelectionEvent;
导入 javax.swing.event.ListSelectionListener;

公共类同义词扩展 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);

}

}

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);

}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文