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 03-12-2008, 12:24 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,637
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Java Swing class capturing output to the console
It is possible to send output to the console from a Swing class as shown in the example:

Code:
public class RedirectedFrame extends JFrame { // Class information public static final String PROGRAM_NAME = "Redirect Frame"; public static final String VERSION_NUMBER = "1.1"; public static final String DATE_UPDATED = "13 April 2007"; public static final String AUTHOR = "Real Gagnon - edited by William Denniss"; private boolean catchErrors; private boolean logFile; private String fileName; private int width; private int height; private int closeOperation; TextArea aTextArea = new TextArea(); PrintStream aPrintStream = new PrintStream( new FilteredStream( new ByteArrayOutputStream())); /** Creates a new RedirectFrame. * From the moment it is created, * all System.out messages and error messages (if requested) * are diverted to this frame and appended to the log file * (if requested) * * for example: * RedirectedFrame outputFrame = * new RedirectedFrame (false, false, null, 700, 600, JFrame.DO_NOTHING_ON_CLOSE); * this will create a new RedirectedFrame that doesn't catch errors, * nor logs to the file, with the dimentions 700x600 and it doesn't * close this frame can be toggled to visible, hidden by a controlling * class by(using the example) outputFrame.setVisible(true|false) * @param catchErrors set this to true if you want the errors to * also be caught * @param logFile set this to true if you want the output logged * @param fileName the name of the file it is to be logged to * @param width the width of the frame * @param height the height of the frame * @param closeOperation the default close operation * (this must be one of the WindowConstants) */ public RedirectedFrame (boolean catchErrors, boolean logFile, String fileName, int width, int height, int closeOperation) { this.catchErrors = catchErrors; this.logFile = logFile; this.fileName = fileName; this.width = width; this.height = height; this.closeOperation = closeOperation; Container c = getContentPane(); setTitle("Output Frame"); setSize(width,height); c.setLayout(new BorderLayout()); c.add("Center" , aTextArea); displayLog(); this.logFile = logFile; System.setOut(aPrintStream); // catches System.out messages if (catchErrors) System.setErr(aPrintStream); // catches error messages // set the default closing operation to the one given setDefaultCloseOperation(closeOperation); Toolkit tk = Toolkit.getDefaultToolkit(); Image im = tk.getImage("myicon.gif"); setIconImage(im); } 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(fileName, true); aWriter.write(aString); aWriter.close(); } } } private 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(); } }
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
Reading a line from console using Scanner class Java Tip Java Tips 0 01-18-2008 12:52 PM
Accessing one class from another class through swing kbyrne AWT / Swing 5 01-03-2008 08:54 AM
Read from console (Scanner Class) hey New To Java 10 12-11-2007 11:11 PM
Help with java console carl New To Java 1 08-06-2007 03:44 PM
how to write the output of the console to a file fred New To Java 1 07-24-2007 03:02 AM


All times are GMT +3. The time now is 01:14 AM.


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