Results 1 to 15 of 15
  1. #1
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default show text file to textArea

    Hello! How can I show the whole content of a txt file to a jtextArea? the s.next() shows only the first word of the txt!

    Thank you..! :-)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by louboulos View Post
    Hello! How can I show the whole content of a txt file to a jtextArea? the s.next() shows only the first word of the txt!
    What is that 's' thing? A Scanner? You should append the entire contents of the file (read line by line?) to the JTextArea.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    yes the s is the variable for Scanner but how can I do this ''Line to Line" thing? with a for loop? and if show how do I determine the length of the file?

  4. #4
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    Here is the Code by the way:

    Java Code:
    import java.awt.Dimension;
    
    import java.awt.Font;
    import java.awt.Rectangle;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import java.io.FileNotFoundException;
    
    import java.util.Scanner;
    
    import javax.swing.JButton;
    import javax.swing.JEditorPane;
    import javax.swing.JFileChooser;
    import javax.swing.JFormattedTextField;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.JTextPane;
    
    public class SearchEng extends JFrame {
        private JLabel searchJL = new JLabel();
        private JTextField searchTF = new JTextField();
        private JButton browseB = new JButton();
        final JFileChooser fc = new JFileChooser();
        private JTextArea textA = new JTextArea();
    
        public SearchEng() {
            try {
                jbInit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private void jbInit() throws Exception {
            this.getContentPane().setLayout( null );
            this.setSize(new Dimension(932, 523));
            searchJL.setText("Search");
            searchJL.setBounds(new Rectangle(25, 0, 95, 25));
            searchJL.setFont(new Font("Tahoma", 1, 16));
            searchTF.setBounds(new Rectangle(120, 0, 535, 35));
            browseB.setText("Browse...");
            browseB.setBounds(new Rectangle(690, 10, 165, 45));
            browseB.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        browseB_actionPerformed(e);
                    }
                });
            textA.setBounds(new Rectangle(220, 150, 510, 195));
            this.getContentPane().add(textA, null);
            this.getContentPane().add(browseB, null);
            this.getContentPane().add(searchTF, null);
            this.getContentPane().add(searchJL, null);
        }
    
        private void browseB_actionPerformed(ActionEvent e) {
          int returnVal = fc.showOpenDialog(null);
            Scanner s;
            try {
                s = new Scanner(fc.getSelectedFile());
                
                  textA.setText(s.nextLine());
                
            } catch (FileNotFoundException f) {
            }
            
            
        }
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

    Default

    Why not use the Scanner to read the whole file? Scanner has a hasNextLine() method that could be used to control a while loop, and inside the loop you could call Scanner's nextLine() method to get the next line and append it to the JTextArea via its append method.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    8

    Default

    Java Code:
    textArea.read(...);
    then you can do it with one line of code and you don't need to worry about looping.

  7. #7
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Java Code:
    textArea.read(...);
    then you can do it with one line of code and you don't need to worry about looping.

    Java Code:
    textA.read(s,null);
    That doesn't work... I don't know the syntax...!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

    Default

    Quote Originally Posted by louboulos View Post
    Java Code:
    textA.read(s,null);
    That doesn't work... I don't know the syntax...!
    Isn't that what the Java API is for? Please feel free to have a look at it. If you're still confused, then come on back.

  9. #9
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Why not use the Scanner to read the whole file? Scanner has a hasNextLine() method that could be used to control a while loop, and inside the loop you could call Scanner's nextLine() method to get the next line and append it to the JTextArea via its append method.
    i tried this:
    Java Code:
                while (s.hasNextLine()){
                    textA.setText(s.nextLine()); }
    but nothing... I don't get it..!

  10. #10
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Isn't that what the Java API is for? Please feel free to have a look at it. If you're still confused, then come on back.
    I already did that..! Nothing specific there or I just can't see it!

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    8

    Default

    Nothing specific there or I just can't see it!
    What are the parameters for the method?

  12. #12
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    What are the parameters for the method?
    Reader,Object

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by louboulos View Post
    Reader,Object
    Is your Scanner s a Reader type object? I don't think so ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  14. #14
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    What about read line into a string variable. concatenate "\n" to it. read another line concatonate to the same variable and add "\n". Run this in a loop. So you have all text into one variable. and then just do JTextArea.setText(<variableName>);
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by FlyNn View Post
    What about read line into a string variable. concatenate "\n" to it. read another line concatonate to the same variable and add "\n". Run this in a loop. So you have all text into one variable. and then just do JTextArea.setText(<variableName>);
    That would be clumsy, the parent class (JTextComponent) can do it all as was outlined by camickr; the OP should just read a bit ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Replies: 3
    Last Post: 05-27-2010, 10:54 AM
  2. Replies: 0
    Last Post: 02-25-2010, 12:30 PM
  3. Appending text to TextArea
    By deepthought015 in forum AWT / Swing
    Replies: 3
    Last Post: 05-01-2009, 02:42 PM
  4. How To add the Text field to the TextArea or TextPane
    By Chintan Patel in forum AWT / Swing
    Replies: 3
    Last Post: 04-08-2009, 02:46 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:35 AM

Posting Permissions

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