Results 1 to 11 of 11
  1. #1
    realahmed8 is offline Member
    Join Date
    Jun 2008
    Posts
    13
    Rep Power
    0

    Default 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:


    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);
    }
    }
    could you please help me, and tell me what to add or to modify,,

    Thank you..

  2. #2
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    How about 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);
    }
    }
    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.
    Last edited by DonCash; 06-02-2008 at 04:08 PM.
    Did this post help you? Please me! :cool:

  3. #3
    realahmed8 is offline Member
    Join Date
    Jun 2008
    Posts
    13
    Rep Power
    0

    Default

    thanks..
    but i still wanna know how to put the content of the text file to the text area..

  4. #4
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    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:

  5. #5
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    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.

  6. #6
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    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. =)
    Thats exactly what I did above. Try running the code in post #2!

    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:

  7. #7
    realahmed8 is offline Member
    Join Date
    Jun 2008
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by DonCash View Post
    Thats exactly what I did above. Try running the code in post #2!

    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);
           	}
    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:
    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);
    sorry for disturb, but could you help me in this..

    thank you very much..

  8. #8
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    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:

  9. #9
    realahmed8 is offline Member
    Join Date
    Jun 2008
    Posts
    13
    Rep Power
    0

    Default

    thank you very much DonCash, you helped me alot..
    and thank you for the helpful tutorials links..

  10. #10
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    No problem. Glad I could help...
    Did this post help you? Please me! :cool:

  11. #11
    fahdouch is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default JTextAraa

    how can i start writing in a jtextarea from the right to left.
    tanks in advance ^^

Similar Threads

  1. Replies: 7
    Last Post: 08-17-2010, 06:00 PM
  2. Replies: 17
    Last Post: 07-13-2009, 05:44 AM
  3. Replies: 1
    Last Post: 07-18-2007, 11:20 PM
  4. Replies: 0
    Last Post: 07-17-2007, 03:30 PM
  5. viewing the contents of a text file in JTextArea
    By warship in forum New To Java
    Replies: 0
    Last Post: 07-17-2007, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •