Results 1 to 13 of 13
  1. #1
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Angry Problem with painting the contents of a file on a JTextArea

    Hi friends!

    Recently I am trying to implement this problem : Paint the contents of a file (text file) on to a JTextArea. For the first look, it seems to work well, but actually when I take a deeper look at the output, I realized that the contents of the file are not fully displayed on the JTextArea. That is, some parts of the text file are not displayed and some parts are displayed clearly.

    After studying the code clearly, I cannot find out any problem and decide to get help from professional :mad:. Here is the code, please help me :mad:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    
    public class TextAreaTest
    {
       public static void main(String[] args) 
       {
          EventQueue.invokeLater(new Runnable()
             {
                public void run() 
                {
    		  try 
                      {
                          TextAreaFrame frame = new TextAreaFrame();
                          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                          frame.setVisible(true);
    		  }
    		  catch(IOException e)
    		  {
    		     e.printStackTrace();
    		  }
                }
             });
       }
    }
    
    class TextAreaFrame extends JFrame 
    {
        private JTextArea textArea;
        public TextAreaFrame() throws IOException
        {
    	setTitle("JIE");
    	setSize(500, 400);
    
    	textArea = new JTextArea(20, 60);	
    	textArea.setLineWrap(true);
            textArea.setEditable(false);
    	textArea.setFont(new Font("Vrinda", Font.BOLD, 23));
    
    	JScrollPane scrollPane = new JScrollPane(textArea);
    
    	add(scrollPane);
    
    	pack();
    
    	FileInputStream fis = new FileInputStream("haha.txt"); // display the contents of the file [B]haha.txt[/B]
    	BufferedReader bd = new BufferedReader(new InputStreamReader(fis));
    
           String line;
           while((line = bd.readLine()) != null)
           {
    	    textArea.append(bd.readLine());
    	    textArea.append("\n");
           }
        }    
    }
    Thank you!
    Last edited by Willi; 10-08-2009 at 09:09 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Don't use those streams for reading text data. Use FileReader/BufferedReader instead.
    Alternatively, read the API specs for the JTextComponent.read method.

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

    Default

    Hi Willi,

    you're skipping lines:
    Java Code:
    while((line = bd.readLine()) != null) // <-----read here
           {
    	    textArea.append(bd.readLine()); // <----read again
    	    textArea.append("\n");
           }
    So you should append line:
    Java Code:
    while((line = bd.readLine()) != null) // <-----read here
           {
    	    textArea.append(line); 
    	    textArea.append("\n");
           }
    And you should consider using a Scanner:
    Java Code:
    Scanner s = new Scanner(new File("haha.txt"));
    while (s.hasNext()) {
    	textArea.append(s.nextLine());
    	textArea.append("\n");
    }
    EDIT: sloooow

  4. #4
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Talking

    Great thanks!

    hi r03198x : actually I used BufferedReader but it still does not work well. It just print a part of the file and some lines are lost. Can you explain for me why? thanks :D

    to PhHein : the best way is to create an object of the class File to hold a file and then read from it using FileReader. Thanks for your reply :D

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

    Default

    Quote Originally Posted by Willi View Post
    to PhHein : the best way is to create an object of the class File to hold a file and then read from it using FileReader.
    Why? Where did you get that from?

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Please read the messages posted already.

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

    Default

    Sorry r035198x, I'm a bit dense today. Rereading the posted messages doesn't give me a clue.

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by PhHein View Post
    Sorry r035198x, I'm a bit dense today. Rereading the posted messages doesn't give me a clue.
    Go away.

    I was just too lazy to quote the OP.

    r035198x(<----Lazy bum.

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

    Default

    Great, just great. Sums up my day :)

  10. #10
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    Why? Where did you get that from?
    I mean for my problem, create a File object is the best way and that is what you advise me to do. Because I just try to make a BufferedReader object and it does not work well. Can you explain for me why? Is this the problem of the buffer? :confused:

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Did you use FileReader with it like I suggested?
    Did you read the API specs for that read method like I suggested?
    Did you read any of the responses posted in this thread?

  12. #12
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    Hey easy brother! What happen to you? I try that program according to your advice, and here is the code, it works perfect.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    
    public class TextAreaTest
    {
       public static void main(String[] args) 
       {
          EventQueue.invokeLater(new Runnable()
             {
                public void run() 
                {
    		  try {
                      TextAreaFrame frame = new TextAreaFrame();
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      frame.setVisible(true);
    		  }
    		  catch(IOException e)
    		  {
    		     e.printStackTrace();
    		  }
                }
             });
       }
    }
    
    class TextAreaFrame extends JFrame 
    {
        JTextArea textArea;
        public TextAreaFrame() throws IOException
        {
    	setTitle("JIE");
    	setSize(500, 400);
    
    	textArea = new JTextArea(20, 60);	
    	textArea.setLineWrap(true);
    	textArea.setFont(new Font("Vrinda", Font.BOLD, 23));
    	JScrollPane scrollPane = new JScrollPane(textArea);
    
    	add(scrollPane);
    
    	pack();
    
    [B]        // CODE CHANGED HERE 
    	File inputFile = new File("haha.txt");
    	FileReader fr = new FileReader(inputFile);[/B]
    
    	int c;
    	while((c = fr.read()) != -1)
    	{
    	    textArea.read(fr, fr.toString());
    	    textArea.append("\n");
    	}
    
    	fr.close();
        }    
    }
    Now i just want to ask why does BufferedReader not work as well as FileReader :(

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

    Default

    A BufferedReader does work better than a file reader.

    The suggestion was to use the JTextComponent.read(...) method, not the BufferedReader.read(...) method. Using the JTextComponent.read() its one line of code. Using the BufferedReader.readln(...) method you need to create your own loop.

Similar Threads

  1. Replies: 17
    Last Post: 07-13-2009, 05:44 AM
  2. Jpanel painting problem
    By kcakir in forum AWT / Swing
    Replies: 3
    Last Post: 04-15-2009, 10:21 PM
  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
  •