Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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 02-04-2008, 10:28 AM
Moderator
 
Join Date: Nov 2007
Posts: 1,637
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Sending output to a Frame
Following example uses IO and AWT package to send the output to the frame.

Code:
public class RedirectedFrame extends Frame { TextArea aTextArea = new TextArea(); PrintStream aPrintStream = new PrintStream( new FilteredStream( new ByteArrayOutputStream())); boolean logFile; RedirectedFrame(boolean logFile) { this.logFile = logFile; System.setOut(aPrintStream); System.setErr(aPrintStream); setTitle("Error message"); setSize(500,300); setLayout(new BorderLayout()); add("Center" , aTextArea); displayLog(); addWindowListener (new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); } } ); } class FilteredStream extends FilterOutputStream { public FilteredStream(OutputStream aStream) { super(aStream); } public void write(byte b[]) throws IOException { String aString = new String(b); aTextArea.append(aString); } public void write(byte b[], int off, int len) throws IOException { String aString = new String(b , off , len); aTextArea.append(aString); if (logFile) { FileWriter aWriter = new FileWriter("error.log", true); aWriter.write(aString); aWriter.close(); } } } public void displayLog() { Dimension dim = getToolkit().getScreenSize(); Rectangle abounds = getBounds(); Dimension dd = getSize(); setLocation((dim.width - abounds.width) / 2, (dim.height - abounds.height) / 2); setVisible(true); requestFocus(); } public static void main(String s[]){ try { // force an exception for demonstration purpose Class.forName("unknown").newInstance(); } catch (Exception e) { // for applet, always RedirectedFrame(false) RedirectedFrame r = new RedirectedFrame(true); e.printStackTrace(); } } }
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
close a frame.. tajinvillage New To Java 5 04-27-2008 11:22 PM
How to place panel into frame vivek_9912 AWT / Swing 2 11-20-2007 12:21 AM
Resize frame lenny AWT / Swing 1 07-30-2007 12:18 AM
Frame Query Daniel AWT / Swing 1 07-05-2007 07:27 PM
Frame problems gary AWT / Swing 2 06-20-2007 02:21 PM


All times are GMT +3. The time now is 10:32 AM.


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