![]() |
|
Display .txt file in java using eclipse - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5) +---- Thread: Display .txt file in java using eclipse (/showthread.php?tid=5825985) |
Display .txt file in java using eclipse - ronnyfhtg - Apr 16, 2018 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.. RE: Display .txt file in java using eclipse - Vanagander - Apr 17, 2018 Please use the code format or atleast indent it properly next time, it's hell to read without indentation. Now my java is rusty but this part at line 104 (for me, third line with text from the bottom): Code TextArea tmpTeks = new JTextArea();You're creating an empty textarea, it won't display anything. |