new problem trying to view the contents of a text file in JTextArea

hello hunter9000.

thanks for the help. i did what you told me to do as much as i possibly could. but when i run the program and press View Order this time, "null" appears in the Compile Messages section of the jGRASP console. what have i done wrong this time? how can i improve my code in order for it to function properly?

import java.awt.BorderLayout;

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

import java.io.*;

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

publicvoid actionPerformed(ActionEvent e){

String s =null;

System.out.println(s +"\n");

//Read from file

try{

String inputFileName ="Order.txt";

File inputFile =new File(inputFileName);

FileInputStream in =new FileInputStream(inputFile);

BufferedReader iS =new BufferedReader(new InputStreamReader(in));

String iL ="";

iL = iS.readLine();

setDescription(iL);

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

}

});

}

/** Set the text description */

publicvoid setDescription(String s){

jtaDescription.setText(s);

}

publicstaticvoid main(String[] args){

new ViewOrder();

}

}

[5543 byte] By [james-mcfaddena] at [2007-11-27 10:24:05]
# 1

You can improve your code by learning how to interpret stack traces and compiler errors. I've offered several times to help you with this, but apparently it's not a route that interests you.....

georgemca at 2007-7-28 17:25:48 > top of Java-index,Java Essentials,Java Programming...
# 2

hello.

i've sorted out that problem with the logon program.

i have done more modifications to the code, which means that i did all that hunter9000 told me to do - to the best of my ability. when i run the program this time, i press the View Order button. when i press the button, no data from the Order.txt file is being displayed in the text area. i definitely need to know where i'm going wrong this time.

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

//Read from file

try{

String inputFileName = "Order.txt";

File inputFile = new File(inputFileName);

FileInputStream in = new FileInputStream(inputFile);

BufferedReader iS = new BufferedReader(new InputStreamReader(in));

String iL = "";

iL = iS.readLine();

setDescription(iL);

iS.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 iL) {

jtaDescription.getPreferredSize();

jtaDescription.setText(iL);

jtaDescription.append(iL);

}

public static void main(String[] args){

new ViewOrder();

}

}

james-mcfaddena at 2007-7-28 17:25:48 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi there,

I took a very quick look at your code. The reason null keeps coming up is that you have declared a String with the value null & keep printing it to the console. If you take out the two lines:

String s = null;

System.out.println(s + "\n");

under the actionPerformed of the button, it will not print out null anymore.

Hope this is what you were looking for.

Good luck!

Guest123a at 2007-7-28 17:25:48 > top of Java-index,Java Essentials,Java Programming...
# 4

I was trying to give you general pointers that would both help with this problem, and every other problem you're going to otherwise be here asking for help with, but no matter

Some people just don't want to be helped I guess......

georgemca at 2007-7-28 17:25:48 > top of Java-index,Java Essentials,Java Programming...
# 5

Try adding JTextArea.revalidate() in your setDescription() method.

Djaunla at 2007-7-28 17:25:48 > top of Java-index,Java Essentials,Java Programming...
# 6

What's wrong with this picture?

pnlBody.add(jtaDescription);

JScrollPane scrollPane = new JScrollPane(jtaDescription);

pnlBody.add(scrollPane, BorderLayout.CENTER);

uncle_alicea at 2007-7-28 17:25:48 > top of Java-index,Java Essentials,Java Programming...