Results 1 to 11 of 11
  1. #1
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default text areas, printstream, and "\n"

    Hey all. I'm almost done with my notepad program - there's just two tiny problems.

    1) I used this code to save my files:

    Java Code:
    Scanner tScan = new Scanner(area.getText());
                output.print(tScan.nextLine());
                while(tScan.hasNextLine()) {
                  output.print("\n" + tScan.nextLine());
                }
    It's working kind of, but for some reason even if I have multiple new lines in my text area when I save it, it outputs them all onto one straight line. This is probably because the text area doesn't log when I start a new line... that's my guess. How do I get around this?

    2) Also - how do I edit the title in the titlebar of my JFileChoosers? I tried to send it as the second parameter, as I had seen done in code I looked up, but it didn't work.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you try to workout with the system properties for the new line? Something like this,

    Java Code:
    System.getProperty("line.separator");

  3. #3
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    Oh dang, I don't think my course/book covered that. Could you explain to me how I would use that?

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

    Default

    what if instead you tried something simple like
    Java Code:
    area.write(output);
    You'd need all the try/catch block stuff, but perhaps this is all you need?

    This allows the JTextArea itself to store its contents into the Writer stream that is passed as a parameter.

  5. #5
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    Thanks man. Just put that in. It's now working - got the spaces/new lines working. But now it's not saving as a txt file anymore, even though I used addChoosableFileFilter with my text filter which only accepts ".txt" files. How do I fix that? Now it's just saving as a generic file :(, though it works when I open it with notepad.

    Also how do I set the title of the file chooser?
    Last edited by diggitydoggz; 12-26-2008 at 05:07 AM.

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

    Default

    But now it's not saving as a txt file anymore,
    not true.

    even though I used addChoosableFileFilter with my text filter which only accepts ".txt" files. How do I fix that?
    Would setFileFilter be more appropriate than addChoosableFileFilter?

    Now it's just saving as a generic file , though it works when I open it with notepad.
    In other words, it's saving it as a text file, but it doesn't have a ".txt" extension. So fix it -- Add the extension. This shouldn't be too hard for you to figure out.

    Also how do I set the title of the file chooser?
    Have you not looked at the JFileChooser API? If you do, I'll bet that you'll also be able to figure this out yourself as well.
    Last edited by Fubarable; 12-26-2008 at 05:48 AM.

  7. #7
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    Thanks. Yeah my bad. Got a little lazy. I figured the last two out. Program complete... onto the next project: hangman.

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

    Default

    You're welcome, and good luck with your hangman project.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by diggitydoggz View Post
    Oh dang, I don't think my course/book covered that. Could you explain to me how I would use that?
    First of all read about the System class.

    Here is the link.

    Keep your eye on getProperty() two overloaded methods.

    Then look at the following list.

    Java System Properties

  10. #10
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    gotcha... I had to add "\r\n" to the front of the string instead of just "\n". I had already finished it using the FileWriter that Fuburable helped me out with but I decided to try to rewrite it using the line.separator instead and it works too. Thanks for the help man... this will come in handy.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 10:26 AM
  2. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  3. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  4. <core:forEach var="" begin="+<%=j%>+">???
    By freddieMaize in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2008, 02:20 AM
  5. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 04:42 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
  •