Results 1 to 12 of 12
  1. #1
    Adrien is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Question Printing text files to a JTextArea

    I have file x.txt that conatins content, now the code I have so far is:

    Java Code:
    		JPanel center = new JPanel();
    		
    		JTextArea inputText = new JTextArea();
    		inputText.setBackground(null);
    		inputText.setEditable(false);
    		
    		JScrollPane scroll = new JScrollPane(inputText);
    
    		mainPane.add(scroll,BorderLayout.CENTER);
    this is just a sample of the file

    So how would I take the contents of x.txt and print them to the JTextArea?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Adrien is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    Fabulous but if I could read the words "and a sample of how you would use it can be seen here..." life would be set....:)

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

    Default

    Quote Originally Posted by Adrien View Post
    Fabulous but if I could read the words "and a sample of how you would use it can be seen here..." life would be set....:)
    The link takes you right to the method you'll need to use to read text in to your text component. If you are unsure how to use Readers and Writers, your best bet is to check out the excellent tutorials on IO at the Sun tutorial site. There you'll find samples and much more.

    Much luck!

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    What's the exact problem? What have you tried?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    Adrien is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    Problem TextArea needs contents of x.txt displayed when called upon.
    what have I tried, nothing cause i didnt know what to call

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    textarea.read(reader, description);
    As described in the API.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    Adrien is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    it should be .write no? for I am WRITING the contents of the txt file to the JTextArea?

  9. #9
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    From mentioned API:

    write

    public void write(Writer out)
    throws IOException

    Stores the contents of the model into the given stream. By default this will store the model as plain text.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  10. #10
    Adrien is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    Ya but - I open and read the contents of the file, how does reading it store it in the text area? like am I basically just reading it and instead of storing it in the buffer I am storing it in a GUI text area?

  11. #11
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    This one method call does it all for you automagically: textarea.read(reader, description);
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    Quote Originally Posted by Adrien View Post
    Ya but - I open and read the contents of the file, how does reading it store it in the text area? like am I basically just reading it and instead of storing it in the buffer I am storing it in a GUI text area?
    No, you are displaying it in the text area. When the program exits the text displayed there disappears into nothingness, so nothing is stored. You need to read the text from the file before displaying it, hence a reader is needed. I suggest that rather than debate this, you try this out on some small GUI app and a simple text file and see what happens.

Similar Threads

  1. Replies: 13
    Last Post: 12-11-2009, 09:43 AM
  2. printing simple text as text on printer
    By Nicholas Jordan in forum Advanced Java
    Replies: 0
    Last Post: 12-01-2008, 02:42 AM
  3. Need Help showing text in JTextArea
    By GuyFawkes in forum AWT / Swing
    Replies: 3
    Last Post: 05-05-2008, 10:19 AM
  4. JTextArea - text align
    By bradder in forum AWT / Swing
    Replies: 1
    Last Post: 11-29-2007, 08:08 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
  •