Search code examples
javaswingjpaneljava.util.scannerevent-dispatching

Text problems with JPanel


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:

  • Why does my text fail to show up, or only display one word? Is it because I don`t specify a layout?
  • Do I need to think about text wrapping?
  • Should I look into JTextArea instead of JPanel, and BufferedReader instead of Scanner?
  • Have I been using the nextLine method from the Scanner correctly?
  • Can I put a command to read a file and display that file`s text in the display method of StoryFrame (I think this would make things a lot easier)?

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

}

ShowIntro

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