Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

using JTextArea in order to view the contents of a text file

807605Jul 11 2007 — edited Jul 12 2007
hello.
i am trying to display the contents of a text file in JTextArea. when i compile the program below, it doesn't compile for me. i get 2 errors (shown below as well). how do i get rid of these errors?
thank you for your help.

----jGRASP exec: javac -g E:\cp4bproject\ViewOrder.java
ViewOrder.java:100: illegal start of expression
public void setDescription(String text) {
^
ViewOrder.java:102: ';' expected
}
^
2 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
 
public class ViewOrder extends JFrame{
   //Declare and create a description panel
   private DescriptionPanel descriptionPanel = new DescriptionPanel();
	
   JPanel pnlText, pnlBody, pnlFooter;
   JButton btnViewOrder;
   JButton btnReturnToOrderSystem;
   JLabel jl;
   Container contentpane;
 
   public ViewOrder(){
      super("View Order");
      contentpane = getContentPane();
      contentpane.setLayout(new BorderLayout());
      pnlText = new JPanel();
      pnlBody = new JPanel();
      pnlFooter = new JPanel();
      jl = new JLabel("Text retrieved from file:");
      btnViewOrder = new JButton("View Order");
      btnReturnToOrderSystem = new JButton("Return to Order System Menu");
      pnlText.add(jl);
      pnlBody.add(descriptionPanel);
      pnlFooter.add(btnViewOrder);
      pnlFooter.add(btnReturnToOrderSystem);
      contentpane.add(pnlText,BorderLayout.NORTH);
      contentpane.add(pnlBody,BorderLayout.CENTER);
      contentpane.add(pnlFooter,BorderLayout.SOUTH);
      pack();
      setVisible(true);
		
      btnViewOrder.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
            String s = null;
				
            //Read from file
            try{
               String inputFileName = "Order.txt";
               File inputFile = new File(inputFileName);
               FileInputStream in = new FileInputStream(inputFile);
               byte bt[] = new byte[(int)inputFile.length()];
               String description = in.read(bt);
               descriptionPanel.setDescription(description);
               s = new String(bt);
               in.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();
   }
}
 
class DescriptionPanel{
   /** Text area for displaying text */
   private JTextArea jtaDescription = new JTextArea(); 
	
   public DescriptionPanel(){
      jtaDescription = new JTextArea();
      pnlBody.add(jtaDescription); 
      jtaDescription.setFont(new Font("Serif", Font.PLAIN, 14));
		
      // Set lineWrap and wrapStyleWord true for the text area
      jtaDescription.setLineWrap(true);
      jtaDescription.setWrapStyleWord(true);
      jtaDescription.setEditable(false);
 
      // Create a scroll pane to hold the text area
      JScrollPane scrollPane = new JScrollPane(jtaDescription);
		
      // Set BorderLayout for the panel, add label and scrollpane
      pnlBody.add(scrollPane, BorderLayout.CENTER); 
		
      /** Set the text description */
      public void setDescription(String text) {
         jtaDescription.setText(text);
      }  
   } 
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 9 2007
Added on Jul 11 2007
3 comments
324 views