problems trying to view the contents of a text file in JTextArea
hello.
Im 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 when it should be. what can i do to get rid of this problem?
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 jta;
Container contentpane;
public ViewOrder(){
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, 14));
// Set lineWrap and wrapStyleWord true for the text area
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
jta.setEditable(false);
jl =new JLabel("Text retrieved from file:");
btnViewOrder =new JButton("View Order");
btnReturnToOrderSystem =new JButton("Return to Order System Menu");
pnlText.add(jl);
JScrollPane jsp =new JScrollPane(jta);
pnlBody.add(jsp);
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(){
publicvoid 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(){
publicvoid actionPerformed(ActionEvent e){
setVisible(false);
//OrderSystem os = new OrderSystem();
//os.setVisible(true);
}
});
}
publicstaticvoid main(String[] args){
new ViewOrder();
}
}
# 5
> > maybe try adding a jta.validate(); after the
> > jta.setText("");
>
> I don't think there's any need to validate the text
> area after calling setText.
>
> Is your file perhaps empty?
> Try adding a few System.out.println, e.g. print out
> each line as it is being read and print out
> sw.toString().
Was only a guess, but I find the entire PrintWriter stuff just too much. All that is not needed. Simply readLine(), then jta.append(<readString>), then jta.append("\n").
# 6
Functioning code follows. The problem was the large values passed to the JTextArea constructor, combined with no sensible layout manager defined for the panel the JTextArea was being added to.
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();
pnlBody.setLayout(new BorderLayout());
pnlFooter = new JPanel();
jta = new JTextArea();
jta.setFont(new Font("Serif", Font.PLAIN, 14));
// Set lineWrap and wrapStyleWord true for the text area
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
jta.setEditable(false);
jl = new JLabel("Text retrieved from file:");
btnViewOrder = new JButton("View Order");
btnReturnToOrderSystem = new JButton("Return to Order System Menu");
pnlText.add(jl);
JScrollPane jsp = new JScrollPane(jta);
pnlBody.add(jsp, BorderLayout.CENTER);
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();
}
}
Message was edited by:
mbmerrill