(Source Code) Java Swing class capturing output to the console
Submitted by guru on Fri, 01/23/2009 - 12:57
Source Code : 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();
}
}
Courtesy:- Java-forums.org
- guru's blog
- Login to post comments
