Search code examples
javafilejtextarea

How do I move text from a file into a text area?


In this code:

protected void open() {
    if (fc.showOpenDialog(Main.this) == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
    }
}

How can I get the contents of file and set it to a text area called textarea?


Solution

  • Use the API provided by all Swing text components to read a file. Something like:

    FileReader reader = new FileReader( file );
    BufferedReader br = new BufferedReader(reader);
    textArea.read( br, null );
    br.close();