Results 1 to 11 of 11
- 06-02-2008, 03:43 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 13
- Rep Power
- 0
How to get the content of text file to write in JTextArea?
Hello,
I have text area and File chooser..
i wanna the content of choosed file to be written into text area..
I have this code:
could you please help me, and tell me what to add or to modify,,Java Code:import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.*; public class Test_Stemmer extends JFrame { public Test_Stemmer() { super("Arabic Stemmer.."); setSize(350, 470); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); Container c = getContentPane(); c.setLayout(new FlowLayout()); JButton openButton = new JButton("Open"); JButton saveButton = new JButton("Save"); JButton dirButton = new JButton("Pick Dir"); JTextArea ta=new JTextArea("File will be written here", 10, 25); JTextArea ta2=new JTextArea("Stemmed File will be written here", 10, 25); final JLabel statusbar = new JLabel("Output of your selection will go here"); // Create a file chooser that opens up as an Open dialog openButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(true); int option = chooser.showOpenDialog(Test_Stemmer.this); if (option == JFileChooser.APPROVE_OPTION) { File[] sf = chooser.getSelectedFiles(); String filelist = "nothing"; if (sf.length > 0) filelist = sf[0].getName(); for (int i = 1; i < sf.length; i++) { filelist += ", " + sf[i].getName(); } statusbar.setText("You chose " + filelist); } else { statusbar.setText("You canceled."); } } }); // Create a file chooser that opens up as a Save dialog saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser chooser = new JFileChooser(); int option = chooser.showSaveDialog(Test_Stemmer.this); if (option == JFileChooser.APPROVE_OPTION) { statusbar.setText("You saved " + ((chooser.getSelectedFile()!=null)? chooser.getSelectedFile().getName():"nothing")); } else { statusbar.setText("You canceled."); } } }); // Create a file chooser that allows you to pick a directory // rather than a file dirButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int option = chooser.showOpenDialog(Test_Stemmer.this); if (option == JFileChooser.APPROVE_OPTION) { statusbar.setText("You opened " + ((chooser.getSelectedFile()!=null)? chooser.getSelectedFile().getName():"nothing")); } else { statusbar.setText("You canceled."); } } }); c.add(openButton); c.add(saveButton); c.add(dirButton); c.add(statusbar); c.add(ta); c.add(ta2); } public static void main(String args[]) { Test_Stemmer sfc = new Test_Stemmer(); sfc.setVisible(true); } }
Thank you..
- 06-02-2008, 04:04 PM #2
How about this:
You might want to think about adding scrollbars to your JTextArea using JTextPane. Suppose it will be OK for short files but once they go outside of the JTextArea boundaries, you cant read the text. Scrollbars will solve this.Java Code:import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import javax.swing.*; public class Test_Stemmer extends JFrame { public Test_Stemmer() { super("Arabic Stemmer.."); setSize(350, 470); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); Container c = getContentPane(); c.setLayout(new FlowLayout()); JButton openButton = new JButton("Open"); JButton saveButton = new JButton("Save"); JButton dirButton = new JButton("Pick Dir"); final JTextArea ta=new JTextArea(10, 25); JTextArea ta2=new JTextArea("Stemmed File will be written here", 10, 25); final JLabel statusbar = new JLabel("Output of your selection will go here"); // Create a file chooser that opens up as an Open dialog openButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(true); int option = chooser.showOpenDialog(Test_Stemmer.this); if (option == JFileChooser.APPROVE_OPTION) { File[] sf = chooser.getSelectedFiles(); String filelist = "nothing"; if (sf.length > 0) filelist = sf[0].getName(); for (int i = 1; i < sf.length; i++) { filelist += ", " + sf[i].getName(); } statusbar.setText("You chose " + filelist); // Read in data file to JTextArea try{ String strLine; File selectedFile = chooser.getSelectedFile(); FileInputStream in = new FileInputStream(selectedFile); BufferedReader br = new BufferedReader(new InputStreamReader(in)); while ((strLine = br.readLine()) != null) { ta.append(strLine + "\n"); } }catch(Exception e){ System.out.println("Ouch, I fell over! " + e); } } else { statusbar.setText("You canceled."); } } }); // Create a file chooser that opens up as a Save dialog saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser chooser = new JFileChooser(); int option = chooser.showSaveDialog(Test_Stemmer.this); if (option == JFileChooser.APPROVE_OPTION) { statusbar.setText("You saved " + ((chooser.getSelectedFile()!=null)? chooser.getSelectedFile().getName():"nothing")); } else { statusbar.setText("You canceled."); } } }); // Create a file chooser that allows you to pick a directory // rather than a file dirButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int option = chooser.showOpenDialog(Test_Stemmer.this); if (option == JFileChooser.APPROVE_OPTION) { statusbar.setText("You opened " + ((chooser.getSelectedFile()!=null)? chooser.getSelectedFile().getName():"nothing")); } else { statusbar.setText("You canceled."); } } }); c.add(openButton); c.add(saveButton); c.add(dirButton); c.add(statusbar); c.add(ta); c.add(ta2); } public static void main(String args[]) { Test_Stemmer sfc = new Test_Stemmer(); sfc.setVisible(true); } }Last edited by DonCash; 06-02-2008 at 04:08 PM.
Did this post help you? Please
me! :cool:
- 06-02-2008, 04:24 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 13
- Rep Power
- 0
thanks..
but i still wanna know how to put the content of the text file to the text area..
- 06-02-2008, 04:26 PM #4
Try that code I just posted!
That puts the content of the text file into the JTextArea.Did this post help you? Please
me! :cool:
- 06-03-2008, 03:44 AM #5
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
You can create a loop until end of file. where in you must get each line of the File and Append it to the JTextArea + "\n" for carriage return. =)
Mind only knows what lies near the heart, it alone sees the depth of the soul.
- 06-03-2008, 10:12 AM #6
Thats exactly what I did above. Try running the code in post #2!You can create a loop until end of file. where in you must get each line of the File and Append it to the JTextArea + "\n" for carriage return. =)
This is what I added:
Java Code:// Read in data file to JTextArea try{ String strLine; File selectedFile = chooser.getSelectedFile(); FileInputStream in = new FileInputStream(selectedFile); BufferedReader br = new BufferedReader(new InputStreamReader(in)); while ((strLine = br.readLine()) != null) { ta.append(strLine + "\n"); } }catch(Exception e){ System.out.println("Ouch, I fell over! " + e); }Did this post help you? Please
me! :cool:
- 06-03-2008, 11:02 AM #7
Member
- Join Date
- Jun 2008
- Posts
- 13
- Rep Power
- 0
thank you very much for your help..
i didn't notice that in your first qoute..
its work fine..
i have tried ScrollBars but also have problems, and also tried ScrollPane but still have problems..
that's the code i have added:
sorry for disturb, but could you help me in this..Java Code:JScrollPane scrollPane2 = new JScrollPane ( ta2 ); scrollPane2.setVerticalScrollBarPolicy ( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); scrollPane2.setHorizontalScrollBarPolicy ( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); scrollPane2.setBorder ( BorderFactory.createLoweredBevelBorder ( ) ); add ( scrollPane2);
thank you very much..
- 06-03-2008, 11:17 AM #8
Sorted:
Java Code:import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import javax.swing.*; public class Test_Stemmer extends JFrame { public Test_Stemmer() { super("Arabic Stemmer.."); setSize(350, 470); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); Container c = getContentPane(); c.setLayout(new FlowLayout()); JButton openButton = new JButton("Open"); JButton saveButton = new JButton("Save"); JButton dirButton = new JButton("Pick Dir"); final JTextArea ta=new JTextArea(10, 25); JTextArea ta2=new JTextArea("Stemmed File will be written here", 10, 25); final JLabel statusbar = new JLabel("Output of your selection will go here"); JScrollPane scrollText = new JScrollPane(ta); // Create a file chooser that opens up as an Open dialog openButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(true); int option = chooser.showOpenDialog(Test_Stemmer.this); if (option == JFileChooser.APPROVE_OPTION) { File[] sf = chooser.getSelectedFiles(); String filelist = "nothing"; if (sf.length > 0) filelist = sf[0].getName(); for (int i = 1; i < sf.length; i++) { filelist += ", " + sf[i].getName(); } statusbar.setText("You chose " + filelist); // Read in data file to JTextArea try{ String strLine; File selectedFile = chooser.getSelectedFile(); FileInputStream in = new FileInputStream(selectedFile); BufferedReader br = new BufferedReader(new InputStreamReader(in)); while ((strLine = br.readLine()) != null) { ta.append(strLine + "\n"); } }catch(Exception e){ System.out.println("Ouch, I fell over! " + e); } } else { statusbar.setText("You canceled."); } } }); // Create a file chooser that opens up as a Save dialog saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser chooser = new JFileChooser(); int option = chooser.showSaveDialog(Test_Stemmer.this); if (option == JFileChooser.APPROVE_OPTION) { statusbar.setText("You saved " + ((chooser.getSelectedFile()!=null)? chooser.getSelectedFile().getName():"nothing")); } else { statusbar.setText("You canceled."); } } }); // Create a file chooser that allows you to pick a directory // rather than a file dirButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int option = chooser.showOpenDialog(Test_Stemmer.this); if (option == JFileChooser.APPROVE_OPTION) { statusbar.setText("You opened " + ((chooser.getSelectedFile()!=null)? chooser.getSelectedFile().getName():"nothing")); } else { statusbar.setText("You canceled."); } } }); c.add(openButton); c.add(saveButton); c.add(dirButton); c.add(statusbar); //c.add(ta); c.add(scrollText); c.add(ta2); } public static void main(String args[]) { Test_Stemmer sfc = new Test_Stemmer(); sfc.setVisible(true); } }Did this post help you? Please
me! :cool:
- 06-03-2008, 11:23 AM #9
Member
- Join Date
- Jun 2008
- Posts
- 13
- Rep Power
- 0
thank you very much DonCash, you helped me alot..
and thank you for the helpful tutorials links..
- 06-03-2008, 11:27 AM #10
No problem. Glad I could help...
Did this post help you? Please
me! :cool:
- 05-24-2010, 08:25 PM #11
Member
- Join Date
- May 2010
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
How can I save the content of textfield in a text file?
By fred in forum Java AppletsReplies: 7Last Post: 08-17-2010, 06:00 PM -
problem trying to display the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 17Last Post: 07-13-2009, 05:44 AM -
problems trying to view the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 1Last Post: 07-18-2007, 11:20 PM -
problem trying to view the contents of a text file in JTextArea
By warship in forum AWT / SwingReplies: 0Last Post: 07-17-2007, 03:30 PM -
viewing the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 0Last Post: 07-17-2007, 02:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks