Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-17-2007, 04:56 PM
Member
 
Join Date: Jul 2007
Posts: 9
warship is on a distinguished road
problem trying to display the contents of a text file in JTextArea
hello.
I’m 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 (i've attached the .txt file to this message). how can i fix this problem?

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();
pnlFooter = new JPanel();
jta = new JTextArea(250, 250);
jta.setFont(new Font("Serif", Font.PLAIN, 12));

// Set lineWrap and wrapStyleWord true for the text area
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
jta.setEditable(false);

// Create a scroll pane to hold the text area
JScrollPane jsp = new JScrollPane(jta);

// Set BorderLayout for the panel, add label and scrollpane
pnlBody.add(jsp, 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);
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();
}
}
Attached Files
File Type: txt Order.txt (72 Bytes, 5 views)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-01-2008, 04:07 AM
Member
 
Join Date: Feb 2008
Location: Oregon, USA
Posts: 24
Bluefox815 is on a distinguished road
Send a message via MSN to Bluefox815
You took the hard way, all you need is a filename, FileReader, and to call the JTextArea's read method.

Code:
File openAs = new File("Order.txt"); FileReader in = new FileReader(openAs); textArea.read(in, openAs.toString());
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Last line in JTextArea wont display Chris.Brown.SPE Advanced Java 5 04-11-2008 03:52 PM
problems trying to view the contents of a text file in JTextArea warship New To Java 1 07-19-2007 01:20 AM
problem trying to view the contents of a text file in JTextArea warship AWT / Swing 0 07-17-2007 05:30 PM
viewing the contents of a text file in JTextArea warship New To Java 0 07-17-2007 04:29 PM


All times are GMT +3. The time now is 11:07 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org