如何在JScrollPane中显示项目列表?

时间:2013-10-16 06:37:08

标签: java swing list jscrollpane jlist

首先,我是一名初学者编程学生,我不太了解很多Java /编程术语,所以如果你能帮助我,请尝试用简单的词语解释。

我正在尝试在JScrollPane中显示单词列表。此列表由类WordList表示。 JScrollPane位于另一个名为WordFinder的类中。

在WordFinder中,代码类似于:

WordList words = new WordList();

// (other GUI code in between)

JScrollPane scrollPane = new JScrollPane(DONT_KNOW);

如果我把'words'放在DONT_KNOW所在的位置,我会收到一条警告:“构造函数JScrollPane(WordList)未定义”。我可以理解,JScrollPane不应该接受这个奇怪的随机类。但那我该放什么呢?我知道您可以将JList放在JScrollPane中,但是如何将WordList类转换为JList(或JScrollPane将接受的内容)?

我不确定这是否有帮助,但这里是WordList类中的代码(我没有写这个;它是给我的):

    import java.io.*;
    import java.net.URL;
    import java.text.Collator;
    import java.util.*;

    /**
     * A WordList is a set of words loaded from a file and searchable by substring.
     * A word is defined as a sequence of letters (upper case or lower case).
     * WordSets ignore alphabetic case when comparing, searching, or sorting.
     */
    public class WordList {
        private List words;

        /*
         * Rep invariant: words != null
         */

    public WordList() {

    }

    public void load(InputStream in) throws IOException {
        Collator c = Collator.getInstance();
        c.setStrength(Collator.PRIMARY);
        Set s = new TreeSet(c);

        StreamTokenizer tok = new StreamTokenizer(new InputStreamReader(in));
        tok.resetSyntax();
        tok.wordChars('a', 'z');
        tok.wordChars('A', 'Z');

        while (tok.nextToken() != StreamTokenizer.TT_EOF) {
            if (tok.ttype == StreamTokenizer.TT_WORD)
                s.add(tok.sval);
        }

        words = new ArrayList(s);
    }

    public List find(String s) {
        if (s.length() == 0) {
            return Collections.unmodifiableList(words);
        }

        s = s.toLowerCase();

        List l = new ArrayList();
        for (Iterator i = words.iterator(); i.hasNext();) {
            String word = (String) i.next();
            if (word.toLowerCase().indexOf(s) != -1)
                l.add(word);
        }
        return l;
    }

    /**
     * Main method. Demonstrates how to use this class.
     * 
     * @param args
     *            Command-line arguments. Ignored.
     */
    public static void main(String[] args) {
        WordList words = new WordList();

        // Create the word list from a resource.
        // The words file must be in the same directory (or jar file directory)
        // as this class.
        URL url = WordList.class.getResource("words.txt");
        if (url == null)
            throw new RuntimeException("Missing resource: words");
        try {
            words.load(url.openStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // Print all the words containing "ph"
        List matches = words.find("holst");
        for (Iterator i = matches.iterator(); i.hasNext();) {
            System.out.println(i.next());
        }
    }
}

3 个答案:

答案 0 :(得分:2)

在JScrollPane中使用JList。

这是一个例子。

    JList list;
    DefaultListModel listModel;
    listModel = new DefaultListModel();
    listModel.addElement("word1");
    listModel.addElement("word2");
    listModel.addElement("word3");

    //Create the list and put it in a scroll pane.
    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);
    list.addListSelectionListener(this);
    list.setVisibleRowCount(5);
    JScrollPane listScrollPane = new JScrollPane(list);

以上示例来自ListDemo.java

在JList上签出Oracle's tutorial

答案 1 :(得分:1)

假设您的单词由Word课程描述,并且您有List<Word>(即java.util.List<>)个单词;

您必须创建列表模型,该模型将成为JList;

的模型

类似的东西:

    // you have list of words - wordList
    List<Word> wordList= new ArrayList<Word>();

    // create list model for JList
    DefaultListModel<Word> model = new DefaultListModel<Word>();
    // add all words from wordList to model
    for(Word word : wordList){
        model.addElement(word);
    }

    // create JList with model - model
    JList<Word> list = new JList<Word>(model);

   // create scroll pane for scrolling JList
    JScrollPane scrollPane = new JScrollPane(list);

如果您的字词是String的实例,则可以Word替换所有String

还有一个提示,您可能已直接创建了列表模型(DefaultListModel<>),用于存储单词列表而不是java.util.List<>。因此,您无需维护两个列表(DefaultListModel<>java.util.List<>)。

答案 2 :(得分:1)

我认为您的自定义类WordList应该继承JList以使其有效(a.e作为参数放到JScrollPane)。

类似的东西:

public class WordList extends JList<Object> {....}

写完之后:

 WordList words = new WordList();

 JScrollPane scrollPane = new JScrollPane(words);