Reputation: 43
Hi I have made a program that reads a text file containing words and adds it to an array. I now need the words to display in the JTextArea I have created but Im not sure how to. The text file contains one word per line, thats how I want the JTextArea to also display them.
Here is the code so far. The JTextArea I have is called textArea (its created in another method)
public static void file() {
List<String> wordList = new ArrayList<String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("data/WordFile.txt"));
String word;
while ((word = br.readLine()) != null) {
wordList.add(word);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
String[] words = new String[wordList.size()];
wordList.toArray(words);
}
Upvotes: 2
Views: 9653
Reputation: 49597
Create a JTextArea object.
As, @Andrew suggested the correct function is JTextArea.append(String)
JTextArea textArea = new JTextArea();
for(String W: Words)
textArea.append(W);
Upvotes: 5
Reputation: 52205
Take a look at this tutorial to see how to use TextAreas. Basically, want you want to do is to iterate over the array and print it's contents through the Event Dispatcher Thread (the thread which takes care of the GUI). This is usually done through the use of the SwingUtils.invokeLater()
Upvotes: 2