Search code examples
javaswingjtextareajfilechooser

How to Display the Contents of a text file in a TextArea


How do you display the contents of a text file in a TextArea when your using JFileChooser.


Solution

  • Your question is unclear, but I am assuming you want to add the JTextArea to the JFileChooser so that it can act like a file preview panel.

    You can add a JTextArea to the JFileChooser by using the setAccessory() method.

    This tutorial on JFileChooser shows how to do something similar where the accessory displays an image from the file rather than text from the file.

    You will need to be careful to deal properly with files that don't contain text, or which are too large, or which cannot be opened due to permission, etc. It will take a good bit of effort to get it right.