0

What's wrong with my code? I'm trying to read text file and then put the text to JTextArea, but its input only consists of the last line of text. What's wrong?

Code:

public void read() {
    int returnVal = fc.showOpenDialog(null);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        pavadinimas = file.getName();
        try {
            FileInputStream fstream = new FileInputStream(fc.getCurrentDirectory() + "/" + pavadinimas);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) != null) {
                tekstas.setText(strLine);
            }
            in.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Tadas
  • 89
  • 1
  • 6

2 Answers2

1

Use the append function instead of settext

Prasanth Louis
  • 4,658
  • 2
  • 34
  • 47
  • 1
    Extend: because setText(...) override the text everytime you use it. Append add the new text to the old text in this component. – pL4Gu33 Aug 31 '14 at 16:49
0

Don't reinvent the wheel. There is no need to write looping code or append you own end-of-line string.

Use the JTextArea.read(...) method.

camickr
  • 321,443
  • 19
  • 166
  • 288