0

I want to transfer content from a text file into a JTextarea. I suppose my code just needs small adjustments but even through research. I am not able to find out, what is wrong. So far it is just displaying an empty JFrame instead of the text of the file.

this.setSize(this.width, this.height);
this.setVisible(true);
this.jScrollPane = new JScrollPane(this.jTextArea);
this.jPanel = new JPanel();
this.jPanel.setOpaque(true);
this.jTextArea.setVisible(true);

try {
    this.jTextArea = new JTextArea();
    this.jTextArea.read(new InputStreamReader(
        getClass().getResourceAsStream("C:\\wrk\\SapCommerceCloud\\src\\SwingUni\\name")),
        null);

} // catch

    this.add(this.jScrollPane);

And the usage:

public static void main(String[] args) {
    new TextFrame(new File("C:\\wrk\\SapCommerceCloud\\src\\SwingUni\\name"), 500, 500);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
David Baur
  • 13
  • 3
  • `''CATCHING EXCEPTIONS HERE''` What does that mean? What does the code do once it's caught one? 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call `Throwable.printStackTrace()` 3) [`JTextComponent.read(Reader,Object)`](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/text/JTextComponent.html#read(java.io.Reader,java.lang.Object)) provides an easy way 2 load text into a Swing text component. – Andrew Thompson May 12 '19 at 15:16
  • You're adding your text area to a JScrollPane, then make it visible, then create a new, different JTextArea, which is never added to any container, but which is filled with the content of a classpath resource. Don't create two text areas. And read the documentation of getResourceAsStream() to understand what kind of path it expects: it's not an absolute file path. – JB Nizet May 12 '19 at 15:19
  • Here is a complete [example](https://stackoverflow.com/a/25691384/230513). – trashgod May 12 '19 at 16:16

1 Answers1

1

You have 2 important issues in this code:

  • You are creating jScrollPane this.jScrollPane = new JScrollPane(this.jTextArea); before reading the file content using jTextArea
  • The method does not work read(new InputStreamReader( getClass().getResourceAsStream("C:\\wrk\\SapCommerceCloud\\src\\SwingUni\\name")), null); Use the one in the following example.

You have to catch the exception to solve the problems

  public class TextAreaDemo extends JFrame {

    private JScrollPane jScrollPane;
    private JTextArea jTextArea ;
    private static final String FILE_PATH="/Users/user/IdeaProjects/StackOverflowIssues/file.txt";

 public TextAreaDemo() {

        try {
            jTextArea = new JTextArea(24, 31);

            jTextArea.read(new BufferedReader(new FileReader(FILE_PATH)), null);

        } catch (Exception e){

            e.printStackTrace();
        }

        jScrollPane = new JScrollPane(this.jTextArea);
        this.add(this.jScrollPane);
        this.setVisible(true);
        this.setSize(400, 200);
    }
    public static void main(String[] args) {
        TextAreaDemo textAreaDemo = new TextAreaDemo();
    }