Reputation: 141
how to import a Text file content to a JTextArea in a Java application using JFileChooser?
Upvotes: 2
Views: 31591
Reputation: 2398
should be something like the following code:
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();
}
The basic logic:
BufferedReader in = new BufferedReader(new FileReader(file));
String line = in.readLine();
while(line != null){
text.append(line + "\n");
line = in.readLine();
}
Upvotes: 7
Reputation: 1
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);
}
}
Upvotes: 0
Reputation: 168825
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);
}
});
}
}
Upvotes: 3
Reputation: 2003
To import the contents of a file into a JTextArea you simply follow these steps!
The above steps are good enough to perform your task. However, when you give it a try, i would edit my post and add a possible solution.
NB: You must note that when you select a file with a JFileChooser, it returns an Object of type File. You should then use the getName()
method provided by the File class to get the name of the file.
Links that might be of help!
JFileChooser
File
Java tutorials on how to use the JFileChooser
Upvotes: 2
Reputation: 156424
Determine the filename given from the FileChooser, read the contents of the file into a String (e.g. using a StringBuilder
), set the contents of the JTextArea to the contents of the buffer using JTextField#setText(String)
.
Upvotes: 1