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

    Default viewing 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?

    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{
                   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
    Last edited by JavaBean; 07-18-2007 at 04:51 PM.

Similar Threads

  1. Replies: 17
    Last Post: 07-13-2009, 05:44 AM
  2. Viewing contents of zip file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-03-2008, 05:16 PM
  3. Viewing contents of JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 03:12 PM
  4. Replies: 1
    Last Post: 07-18-2007, 11:20 PM
  5. Replies: 0
    Last Post: 07-17-2007, 03:30 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
  •