Results 1 to 18 of 18
  1. #1
    warship is offline Member
    Join Date
    Jul 2007
    Posts
    9
    Rep Power
    0

    Default problem trying to display the contents of a text file in JTextArea

    hello.
    Iím having difficulty trying to view the contents of a text file in a JTextArea. when i run the following program, i press the View Order button. but no data from the Order.txt file is being displayed in the text area (i've attached the .txt file to this message). how can i fix this problem?

    import java.awt.BorderLayout;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;

    public class ViewOrder extends JFrame{
    JPanel pnlText, pnlBody, pnlFooter;
    JButton btnViewOrder;
    JButton btnReturnToOrderSystem;
    JLabel jl;
    JTextArea jta;
    Container contentpane;

    public ViewOrder(){
    super("View Order");
    contentpane = getContentPane();
    contentpane.setLayout(new BorderLayout());
    pnlText = new JPanel();
    pnlBody = new JPanel();
    pnlFooter = new JPanel();
    jta = new JTextArea(250, 250);
    jta.setFont(new Font("Serif", Font.PLAIN, 12));

    // Set lineWrap and wrapStyleWord true for the text area
    jta.setLineWrap(true);
    jta.setWrapStyleWord(true);
    jta.setEditable(false);

    // Create a scroll pane to hold the text area
    JScrollPane jsp = new JScrollPane(jta);

    // Set BorderLayout for the panel, add label and scrollpane
    pnlBody.add(jsp, BorderLayout.CENTER);

    jl = new JLabel("Text retrieved from file:");
    btnViewOrder = new JButton("View Order");
    btnReturnToOrderSystem = new JButton("Return to Order System Menu");
    pnlText.add(jl);
    pnlFooter.add(btnViewOrder);
    pnlFooter.add(btnReturnToOrderSystem);
    contentpane.add(pnlText,BorderLayout.NORTH);
    contentpane.add(pnlBody,BorderLayout.CENTER);
    contentpane.add(pnlFooter,BorderLayout.SOUTH);
    setSize(500, 500);
    setVisible(true);

    btnViewOrder.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    //Read from file
    try{
    FileInputStream in = new FileInputStream("Order.txt");
    BufferedReader iS = new BufferedReader(new InputStreamReader(in));
    StringWriter sw = new StringWriter();
    PrintWriter out = new PrintWriter(sw);
    String il;

    while ((il = iS.readLine()) != null){
    out.println(il);
    }

    out.flush();
    jta.setText(sw.toString());

    in.close();
    iS.close();
    sw.close();
    out.close();
    }
    catch(java.io.IOException ex){
    System.out.println("Cannot read from file");
    }
    }
    });

    btnReturnToOrderSystem.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    setVisible(false);
    //OrderSystem os = new OrderSystem();
    //os.setVisible(true);
    }
    });
    }

    public static void main(String[] args){
    new ViewOrder();
    }
    }
    Attached Files Attached Files

  2. #2
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    You took the hard way, all you need is a filename, FileReader, and to call the JTextArea's read method.

    Java Code:
    File openAs = new File("Order.txt");
    FileReader in = new FileReader(openAs);
    textArea.read(in, openAs.toString());

  3. #3
    chec69 is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default

    Dude So many thanks !! :D this helps a lot
    Last edited by chec69; 03-03-2009 at 03:09 PM.

  4. #4
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    You're welcome. However, try not to post on threads that are a year old.

  5. #5
    tskarthic is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Unhappy

    Hello Friend,

    I have tried to store the file content into JTextArea using this code snippet. But nothing displayed in the JTextArea.

    Quote Originally Posted by Bluefox815 View Post
    You took the hard way, all you need is a filename, FileReader, and to call the JTextArea's read method.

    Java Code:
    File openAs = new File("Order.txt");
    FileReader in = new FileReader(openAs);
    textArea.read(in, openAs.toString());

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

    Default

    then you have a bug somewhere. How about starting your own new thread on this and posting a small compilable program that tries to do what you want here and nothing more -- tries to read a file and display it in a JTextArea, and we'll see if we can help you. Before posting code though, please read the FAQ regarding use of code tags.

  7. #7
    tskarthic is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default Display content into jTextarea.

    Hello Friend,

    I have pasted the sample program which i use.
    The only markable difference is i am using huge size files to display. The size may be morethan 100 MB. Please help me out.:confused:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    
    public class ViewOrder extends JFrame{
    JPanel pnlText, pnlBody, pnlFooter;
    JButton btnViewOrder;
    JButton btnReturnToOrderSystem;
    JLabel jl;
    JTextArea jta;
    Container contentpane;
    
    public ViewOrder(){
    super("View Order");
    contentpane = getContentPane();
    contentpane.setLayout(new BorderLayout());
    pnlText = new JPanel();
    pnlBody = new JPanel();
    pnlFooter = new JPanel();
    jta = new JTextArea(250, 250);
    jta.setFont(new Font("Serif", Font.PLAIN, 12));
    
    // Set lineWrap and wrapStyleWord true for the text area
    jta.setLineWrap(true);
    jta.setWrapStyleWord(true);
    jta.setEditable(false);
    
    // Create a scroll pane to hold the text area
    JScrollPane jsp = new JScrollPane(jta);
    
    // Set BorderLayout for the panel, add label and scrollpane
    pnlBody.add(jsp, BorderLayout.CENTER);
    
    jl = new JLabel("Text retrieved from file:");
    btnViewOrder = new JButton("View Order");
    btnReturnToOrderSystem = new JButton("Return to Order System Menu");
    pnlText.add(jl);
    pnlFooter.add(btnViewOrder);
    pnlFooter.add(btnReturnToOrderSystem);
    contentpane.add(pnlText,BorderLayout.NORTH);
    contentpane.add(pnlBody,BorderLayout.CENTER);
    contentpane.add(pnlFooter,BorderLayout.SOUTH);
    setSize(500, 500);
    setVisible(true);
    
    btnViewOrder.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    //Read from file
    try{
    File openAs = new File("D://mgc12ux_network.log");
    // This file is huge in size. More than 100 MB
    FileReader in = new FileReader(openAs);
    jta.read(in, openAs.toString());
    }
    catch(java.io.IOException ex){
    System.out.println("Cannot read from file");
    }
    }
    });
    
    btnReturnToOrderSystem.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    setVisible(false);
    //OrderSystem os = new OrderSystem();
    //os.setVisible(true);
    }
    });
    }
    
    public static void main(String[] args){
    new ViewOrder();
    }
    }

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

    Default

    Friend, I will gladly try to help you if you follow our instructions. Again, start a new thread on this, and don't hijack somebody else's thread.

  9. #9
    mostafa-sky is offline Member
    Join Date
    Mar 2009
    Location
    Yemen - Taiz
    Posts
    5
    Rep Power
    0

    Default

    hi all,
    I have simple solution,
    it is function read the content of any file,then display it on the JtextArea
    or TextArea.

    Java Code:
    private void setTextContent(String Path)
    	{	
    		try
    		{
    				java.io.DataInputStream o=new java.io.DataInputStream(	
    				new java.io.FileInputStream(Path));
    			String buf="",ur_text="";
    			while((buf=o.readLine())!=null)
    			{
    				ur_text+=buf;
    			}
    			// Now the ur_text ready to  displayed on the text elemnet 
                //this.jta.setText(ur_text);
    			System.out.println(ur_text);
    		}
    		catch(java.io.IOException ioe)
    		{
    			ioe.printStackTrace();
    		}
        }
    that's it :)

  10. #10
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    Your function is not very good. In my opinion, if you have to edit a function to get what you want out of it, the function needs to be rewritten. What you might do differently is make the function return the "ur_text" string and set the JTextArea's text content with the returned string. I've written an equivalent function with different code, only two lines need to be changed in yours to make it return a string.

    Java Code:
    private String getFileContents(String path) throws java.io.IOException {
    	try {
    		StringBuilder contents = new StringBuilder(), line;
    		java.io.BufferedReader in = new java.io.BufferedReader(java.io.FileReader(path));
    		while ((line = in.readLine()) != null) {
    			contents.append(line); // this is faster than "String += String"
    		}
    	} catch (FileNotFoundException fnfe) {
    		System.err.println("The file \"" + path + "\" could not be found.");
    		return null;
    	}
    	return contents.toString();
    }
    
    JTextArea textArea = new JTextArea();
    textArea.setText(getFileContents("files/textfile.txt"));

  11. #11
    mostafa-sky is offline Member
    Join Date
    Mar 2009
    Location
    Yemen - Taiz
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    contents.append(line); // this is faster than "String += String"
    friend Bluefox815,you have a point
    and this v.good notice
    ---------------------

  12. #12
    hello_kolkata is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default Please Help...

    Hi all I'm New , I'm face same problem when i open more than 10MB Text file but when I open 7MB Text file it's open fine . I dont understand please help me ....


    Hello_Kolkata

  13. #13
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    Hi,
    Small changes in ur code.

    rows and columns are more .
    I have made it as 50,50 to make it scroll.
    And then StringWriters getBuffer() method i called on that i called toString to get the text.Run this code and check once.
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    
    public class ViewOrder extends JFrame{
    JPanel pnlText, pnlBody, pnlFooter;
    JButton btnViewOrder;
    JButton btnReturnToOrderSystem;
    JLabel jl;
    JTextArea jta;
    Container contentpane;
    
    public ViewOrder(){
    super("View Order");
    contentpane = getContentPane();
    contentpane.setLayout(new BorderLayout());
    pnlText = new JPanel();
    pnlBody = new JPanel();
    pnlFooter = new JPanel();
    [B]jta = new JTextArea(50,50);[/B]
    jta.setFont(new Font("Serif", Font.PLAIN, 12));
    
    // Set lineWrap and wrapStyleWord true for the text area
    jta.setLineWrap(true);
    jta.setWrapStyleWord(true);
    jta.setEditable(false);
    
    // Create a scroll pane to hold the text area
    JScrollPane jsp = new JScrollPane(jta);
    
    // Set BorderLayout for the panel, add label and scrollpane
    pnlBody.add(jsp, BorderLayout.CENTER); 
    
    jl = new JLabel("Text retrieved from file:");
    btnViewOrder = new JButton("View Order");
    btnReturnToOrderSystem = new JButton("Return to Order System Menu");
    pnlText.add(jl);
    pnlFooter.add(btnViewOrder);
    pnlFooter.add(btnReturnToOrderSystem);
    contentpane.add(pnlText,BorderLayout.NORTH);
    contentpane.add(pnlBody,BorderLayout.CENTER);
    contentpane.add(pnlFooter,BorderLayout.SOUTH);
    setSize(500, 500);
    setVisible(true);
    
    btnViewOrder.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    //Read from file
    try{
    FileInputStream in = new FileInputStream("Order.txt");
    BufferedReader iS = new BufferedReader(new InputStreamReader(in)); 
    StringWriter sw = new StringWriter(); 
    PrintWriter out = new PrintWriter(sw);
    String il;
    
    while ((il = iS.readLine()) != null){
    out.println(il); 
    } 
    System.out.println(sw.getBuffer().toString());
    
    [B]jta.setText(sw.getBuffer().toString()); [/B]
    
    out.flush(); 
    
    in.close();
    iS.close();
    sw.close();
    out.close(); 
    } 
    catch(java.io.IOException ex){
    System.out.println("Cannot read from file");
    }
    }
    });
    
    btnReturnToOrderSystem.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    setVisible(false);
    //OrderSystem os = new OrderSystem();
    //os.setVisible(true);
    }
    });
    }
    
    public static void main(String[] args){
    new ViewOrder();
    }
    }
    Ramya:cool:

  14. #14
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    VERY old thread people... time to leave it.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  15. #15
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    Hi Singing Boyo,
    Great catch Man!!!!!!!!!!!!!!!!!!
    I have not checked the date of the thread.........
    TnxD:D

    -Regards
    Ramya
    Ramya:cool:

  16. #16
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    449
    Rep Power
    6

    Default

    However this thread is old,Can we display the document file contents into TextArea with out modifications.??
    Mak
    (Living @ Virtual World)

  17. #17
    hello_kolkata is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default

    Thank u for reply
    when i view below 8mb file it's open fine but when I view more than 8mb ... nothing happened... I am waiting about 1/2 hour but still same ...
    I am using NetBeans IDE 6.5.1
    Please help me ....

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

    Default

    Again, let's not hijack an old thread for this.

    I'm locking this old thread. hello kolkata, if you still have a question, please start a new thread, and please post enough information so that your question is possible to answer. Currently we see no code, no error messages.

Similar Threads

  1. [SOLVED] Last line in JTextArea wont display
    By Chris.Brown.SPE in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2008, 01:52 PM
  2. Replies: 1
    Last Post: 07-18-2007, 11:20 PM
  3. Replies: 0
    Last Post: 07-17-2007, 03:30 PM
  4. 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
  •