I am trying to display a particular text file in JTextArea but eclipse keeps showing me an error.
this the code :
package readFile;
import java.awt.EventQueue;
import java.io.*;
import java.nio.file.Path;
import java.util.StringTokenizer;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.text.JTextComponent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class frmReadFile {
JFileChooser dialog = new JFileChooser();
private JFrame frmFrmreadfile;
private JTextField txtPath;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frmReadFile window = new frmReadFile();
window.frmFrmreadfile.setVisible(true);
} catch (Exception e2) {
e2.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public frmReadFile() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmFrmreadfile = new JFrame();
frmFrmreadfile.setTitle("frmReadFile");
frmFrmreadfile.setBounds(100, 100, 450, 300);
frmFrmreadfile.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE);
frmFrmreadfile.getContentPane().setLayout(null);
txtPath = new JTextField();
txtPath.setBounds(10, 11, 272, 27);
frmFrmreadfile.getContentPane().add(txtPath);
txtPath.setColumns(10);
JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int pFile = dialog.showOpenDialog(null);
if (pFile == JFileChooser.APPROVE_OPTION) {
File file = dialog.getSelectedFile();
try {
txtPath.setText("" + file);
} catch (Exception e) {
System.out.println("Error : " + e.getMessage());
}
}
}
});
btnBrowse.setBounds(319, 11, 89, 23);
frmFrmreadfile.getContentPane().add(btnBrowse);
JButton btnTampil = new JButton("Tampil Teks");
btnTampil.addActionListener(new ActionListener() {
private JTextComponent tmpTeks;
public void actionPerformed(ActionEvent e) {
tmpTeks.setText(null);
String path = txtPath.getText();
File file = new File(path);
try {
FileInputStream fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(strLine, ".");
tmpTeks.setText(tmpTeks.getText() + st.nextToken() + "\n");
}
in.close();
} catch (Exception e2) {
System.out.println("Error : " + e2.getMessage());
}
}
});
btnTampil.setBounds(10, 49, 89, 23);
frmFrmreadfile.getContentPane().add(btnTampil);
JTextArea tmpTeks = new JTextArea();
tmpTeks.setBounds(10, 81, 398, 168);
frmFrmreadfile.getContentPane().add(tmpTeks);
}
}
when i push buttom btnTampil teks not show, any error in my code????
need help please..