it fails because you never call a method to use the text in your displaytext method
public void displayText(String text) {
mylabel.setText(text);
}
Question
After thinking I was on course to solving a problem with making text (read from a file) appear in a JPanel, I`m frustratingly back to square one.
The code is below. The result is just a blank screen 400x500 screen. Some combinations of using nextLine() + nextLine() as displayText commands result in one word coming up from the file (the word has been different multiple times). This makes me wonder: do I need code that deals with text wrapping? The textfile itself is in paragraphs, and as such, I thought that sf.displayText should say sf.displayText(reader.next() + reader.nextline() + reader.nextline(), and have tried other combinations, but this may be confusing the while parameters. Have also tried a different textfile with simple sentences, no paragraphs, but again, nothing comes up.
Having looked online, I have found that layouts may be an issue, and that alternative options may be BufferedReader or using JTextArea. Browsing through Big Java didn`t provide anything I felt I could take, as all discussion on the scanner went towards integers, whereas the file I want read is prose. I also tried putting a small piece of text in the code itself and cancelling out everything else below it to see if I could transfer text from the code to the JPanel:
StoryFrame sf = new StoryFrame();
sf.displayText("Life is beautiful");
but still nothing came up. Ultimately, I want to put text from a file into a JPanel, and have each paragraph come up 5 seconds after the one before. So ultimately, my questions are:
I know it`s a lot, so any answers to any of the questions would be greatly appreciated, thank you. Tom
import javax.swing.JFrame;
import javax.swing.JLabel;
public class StoryFrame extends JFrame {
private JLabel mylabel;
public StoryFrame() {
setTitle("見張ってしながら...");
setSize(400,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
mylabel = new JLabel();
this.add(mylabel);
setVisible(true);
}
public void displayText(String text) {
JLabel storyText = new JLabel();
add(storyText);
}
}
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
class ShowIntro {
public static void main(String args[])
throws IOException {
StoryFrame sf = new StoryFrame();
Scanner reader = new Scanner(new File("Try.txt"));
while (reader.hasNextLine()) {
//String line = in.nextLine() Not sure whether this would contribute, I doubt it does though
sf.displayText(reader.next());
//sf.displayText(reader.next() + reader.nextLine() + reader.nextLine()); was also attempted.
try {
Thread.sleep(5000);
} catch (InterruptedException e) { }
}
}
}
Solution
it fails because you never call a method to use the text in your displaytext method
public void displayText(String text) {
mylabel.setText(text);
}
OTHER TIPS
You are also reading the file one word at a time:
sf.displayText(reader.next());
should be:
sf.displayText(reader.nextLine());
if you want to read upto the next newline character.
Even though this was not in the original question to satisfy some of the comments below here is a modified version of the program
package com.vincentramdhanie.kitchensink;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class StoryFrame extends JFrame {
private JTextArea area;
public StoryFrame() {
setTitle("見張ってしながら...");
setSize(400,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
area = new JTextArea(10, 30);
area.setEditable(false);
area.setCursor(null);
area.setOpaque(false);
area.setFocusable(false);
this.add(area);
setVisible(true);
}
public void displayText(String text) {
area.setText(text);
}
public static void main(String args[])
throws IOException {
StoryFrame sf = new StoryFrame();
Scanner reader = new Scanner(new File("Try.txt"));
while (reader.hasNextLine()) {
String line = reader.nextLine();
sf.displayText(line);
try {
//to skip blank lines. If the line has no non-space characters then do not sleep
if(!line.trim().equals("")){
Thread.sleep(5000);
}
} catch (InterruptedException e) { }
}
}
}