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);
}
}
}