To import the contents of a file into JTextArea, you simply follow these steps!
- Create a frame and add a JTextArea to it.
- You declare and initialize JFileChooser.
- You add a listener to JFileChooser.
- In your actionPerformed you should take the file that was selected and pass it to the method that will read this file (see below).
- In this method, you open the file reader and read the contents of the file in turn. As you do this, you add each line to JTextArea.
- When you get to the end of the file, you close the file reader.
- Run the program, and you should be fine.
The above steps are good enough to complete your task. However, when you give it a try, I would edit the message and add a possible solution.
Note. You should notice that when you select a file with JFileChooser, it returns an object of type File. Then you must use the getName() method provided by the File class to get the file name.
Links that may be helpful!
JFileChooser
File
Java tutorials on how to use JFileChooser
source share