How to import text file contents into JTextArea in Java application?

How to import text file contents into JTextArea in a Java application using JFileChooser?

+1
source share
5 answers

should look something like this:

JFileChooser chooser = new JFileChooser(); int returnVal = chooser.showOpenDialog(null); //replace null with your swing container File file; if(returnVal == JFileChooser.APPROVE_OPTION) file = chooser.getSelectedFile(); } JTextArea text = new JTextArea(); BufferedReader in = new BufferedReader(new FileReader(file)); String line = in.readLine(); while(line != null){ text.append(line + "\n"); line = in.readLine(); } 

Luke

+7
source

Document Viewer

 import java.awt.BorderLayout; import java.awt.event.*; import javax.swing.*; import java.io.File; class DocumentViewer { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { final JFrame f = new JFrame("Document Viewer"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JFileChooser fileChooser = new JFileChooser(); JPanel gui = new JPanel(new BorderLayout()); final JEditorPane document = new JEditorPane(); gui.add(new JScrollPane(document), BorderLayout.CENTER); JButton open = new JButton("Open"); open.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { int result = fileChooser.showOpenDialog(f); if (result==JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { document.setPage(file.toURI().toURL()); } catch(Exception e) { e.printStackTrace(); } } } }); gui.add(open, BorderLayout.NORTH); f.setContentPane(gui); f.pack(); f.setSize(400,300); f.setLocationByPlatform(true); f.setVisible(true); } }); } } 
+3
source

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

+2
source

Define the file name specified in FileChooser, read the contents of the file in String (for example, using StringBuilder ), set the contents of JTextArea to the contents of the buffer using JTextField#setText(String) .

+1
source
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser jf = new JFileChooser(); final JEditorPane document = new JEditorPane(); int returnval=jf.showDialog(this, null); File file = null; if(returnval == JFileChooser.APPROVE_OPTION) file = jf.getSelectedFile(); String str ; try { byte bt[]= Files.readAllBytes(file.toPath()); str=new String(bt,"UTF-8"); System.out.println(str); } catch (IOException ex) { Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex); } } 
0
source

Source: https://habr.com/ru/post/1484758/


All Articles