Results 1 to 1 of 1
-
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:
Java 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(); } }
Similar Threads
-
Reading a line from console using Scanner class
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 11:52 AM -
Accessing one class from another class through swing
By kbyrne in forum AWT / SwingReplies: 5Last Post: 01-03-2008, 07:54 AM -
Read from console (Scanner Class)
By hey in forum New To JavaReplies: 10Last Post: 12-11-2007, 10:11 PM -
Help with java console
By carl in forum New To JavaReplies: 1Last Post: 08-06-2007, 02:44 PM -
how to write the output of the console to a file
By fred in forum New To JavaReplies: 1Last Post: 07-24-2007, 02:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks