Java text program w/ Swing (help)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Java text program w/ Swing (help)

  1. #1
    Join Date
    Feb 2004
    Posts
    11

    Question Java text program w/ Swing (help)

    Alright, here is the deal:

    I am making a program designed to display a text in some JPanel-type window. The user clicks on the menu and selects the desired text, the application opens the text file residing in the install folder and displays it for the user.

    My problem: It prints the text in one line horizontally and does not make a new line when it reaches the end of the screen. It just keeps going, displaying a 10 page text in one long horizontal line! How can I get it to print horizontally, starting new lines at the end of each line?

    If the code needs to be seen to solve this problem, I will gladly attach it! Just let me know.

    Thanks in advance.

    Manilla

  2. #2
    Join Date
    Feb 2004
    Posts
    11
    Here is the actual code that I have so far.
    Attached Files Attached Files

    Manilla

  3. #3
    Join Date
    Feb 2004
    Posts
    11
    There's quite a bit of errors in your code so I took the liberty to create a shortened and simplified version of it just to give you an idea of what needs to be done.

    Some notes:

    1) In several areas, you used JPanel when it should've been a JFrame( or more appropriately, a JDialog)

    2) text.setText() should be text.append() instead

    3) Be aware that files names with spaces may cause some problems. Instead of spaces, use underscores.

    4) Look up HashMap, and JDialog on the at:

    http://java.sun.com/j2se/1.4.2/search.html

    -Ernest

  4. #4
    Join Date
    Feb 2004
    Posts
    11

    attachment

    here's the attachment
    Attached Files Attached Files

  5. #5
    Join Date
    Feb 2004
    Posts
    11
    awesome, thank you, I am going to go test this out now.

    Manilla

  6. #6
    Join Date
    Feb 2004
    Posts
    11
    althought you made my program much more efficient, the problem that I was having still remains. I would like it to start a new line at the end of the screen and continue going. Think of it as word wrapping. Any help would be greatly appreciated.

    Thank you

    Manilla

  7. #7
    Join Date
    Feb 2004
    Posts
    11
    It seems that you'll need to retrieve the line.separator property from your system in order to properly enter new lines in a JTextFied. Read the explanation here:
    http://leepoint.net/notes-java/30GUI...40newline.html

    You should modify your line read/write routine to:

    Code:
    BufferedReader br = new BufferedReader(new FileReader(file));
    
    String line = null;
    String newline = System.getProperty("line.separator");
    while((line = br.readLine()) !=null) {
        text.append(line+newline); 		
    }
    Let me know how it goes.

    -Ernest

  8. #8
    Join Date
    Feb 2004
    Posts
    11
    Thank you, ecriss. You have solved my problems. That sun systems site that you told me to look stuff up on is extremely useful. I found an even easier to word wrap through this database. All one must do is use the boolean commands:

    "setLineWrap(true)"
    and
    "setWrapStyleWord(true)"

    Thank you again.

    Manilla

  9. #9
    Join Date
    Feb 2004
    Posts
    808
    well, youre both right.. follow ecriss' suggestion and you will have the actual hard-coded linewraps that are in the file preserved.. follow your own suggestion, and any lines too long will wrap at the textfield edge.. you should do both to be honest.. just doing ecriss' will cause lines to disappear off screen if the jtextfield is made small, just doing yours will ploduce a single block of very-hard-to-read text
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center