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

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.*;

publicclass ViewOrderextends 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(){

publicvoid 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[] =newbyte[(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(){

publicvoid actionPerformed(ActionEvent e){

setVisible(false);

//OrderSystem os = new OrderSystem();

//os.setVisible(true);

}

});

}

publicstaticvoid 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 */

publicvoid setDescription(String text){

jtaDescription.setText(text);

}

}

}

[6450 byte] By [james-mcfaddena] at [2007-11-27 10:16:21]
# 1

You can't declare a method inside a constructor. You need to fix the braces in that class.

hunter9000a at 2007-7-28 15:45:06 > top of Java-index,Java Essentials,Java Programming...
# 2

i done what you told me to do. but when i try to compile the code, i get 4 errors. how do i get rid of these errors?

-jGRASP exec: javac -g E:\cp4bproject\ViewOrder.java

ViewOrder.java:35: cannot find symbol

symbol : method add(DescriptionPanel)

location: class javax.swing.JPanel

pnlBody.add(descriptionPanel);

^

ViewOrder.java:54: incompatible types

found: int

required: java.lang.String

String description = in.read(bt);

^

ViewOrder.java:85: cannot find symbol

symbol : variable pnlBody

location: class DescriptionPanel

pnlBody.add(jtaDescription);

^

ViewOrder.java:97: cannot find symbol

symbol : variable pnlBody

location: class DescriptionPanel

pnlBody.add(scrollPane, BorderLayout.CENTER);

^

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

}

}

james-mcfaddena at 2007-7-28 15:45:06 > top of Java-index,Java Essentials,Java Programming...
# 3

hello hunter9000.

i did what you told me to do as much as possible. but when i compile the properly modified code ,i get 2 errors (errors + code are shown below). how do i fix these errors?

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 jtaDescription = new JTextArea();

Container contentpane;

public ViewOrder(){

super("View Order");

contentpane = getContentPane();

contentpane.setLayout(new BorderLayout());

pnlText = new JPanel();

pnlBody = new JPanel();

pnlFooter = new JPanel();

jtaDescription = new JTextArea();

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

pnlBody.add(jtaDescription);

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

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

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

}

});

}

/** Set the text description */

public void setDescription(String text) {

jtaDescription.setText(text);

}

public static void main(String[] args){

new ViewOrder();

}

}

james-mcfaddena at 2007-7-28 15:45:06 > top of Java-index,Java Essentials,Java Programming...