Reputation: 1
First of all, I am a beginner programming student and I don't know a lot of Java/programming jargon, so if you are able to help me, please try to explain in simpler words.
I am trying to display a list of words in a JScrollPane. This list is represented by the class WordList. The JScrollPane is in another class called WordFinder.
In WordFinder, the code is something like:
WordList words = new WordList();
// (other GUI code in between)
JScrollPane scrollPane = new JScrollPane(DONT_KNOW);
If I put 'words' where DONT_KNOW is, I get a warning saying "The constructor JScrollPane(WordList) is undefined". I can understand that, a JScrollPane isn't supposed to accept this weird random class. But what do I put there then? I understand that you can put a JList in a JScrollPane, but how do I transform the WordList class into a JList (or something that JScrollPane will accept)?
I'm not sure if this helps, but here is the code in the class WordList (I didn't write this; it was given to me):
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());
}
}
}
Upvotes: 0
Views: 8593
Reputation: 10220
Use JList inside the JScrollPane.
Here's the example.
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);
The above example is from ListDemo.java
Checkout Oracle's tutorial on JList.
Upvotes: 3
Reputation: 3390
Assuming your word is described by Word
class and you have List<Word>
(i.e. java.util.List<>
) of words;
You have to create list model which will be model for JList
;
something like:
// 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);
If your word is instance of String
, you can replace all Word
by String
.
One more tip, you may have directly created list model (DefaultListModel<>
) for storing list of words instead of java.util.List<>
. So you will not have to maintain two lists (DefaultListModel<>
and java.util.List<>
).
Upvotes: 1
Reputation: 77904
I think your custom class WordList
should inherit JList
to make it work (a.e put as parameter to JScrollPane
).
Something like:
public class WordList extends JList<Object> {....}
After you can write:
WordList words = new WordList();
JScrollPane scrollPane = new JScrollPane(words);
Upvotes: 1