-
ConsoleWindow.init()
The following code should display a window and write to:
System.setOut()
System.setErr()
1. I didn't find any log files or see anything displayed in the console, where do these methods write?
2. ConsoleWindow.init() has to be called from the main method, whilst somehow I am under the impression that this is called automatically.
3. ConsoleWindow.init() should call a window that displays debug messages. However, no such window appears.
Any ideas?
:confused:
Code:
[B]ConsoleWindow.java1[/B]
package homenetwork.bkr.training;
import javax.swing.*;
import java.io.*;
/** A window that displays the bytes sent to System.out and System.err
*
*/
public class ConsoleWindow {
public void init()
{
JFrame frame = new JFrame();
frame.setTitle("Console Window");
final JTextArea output = new JTextArea();
output.setEditable(false);
frame.add(new JScrollPane(output));
frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
frame.setLocation(DEFAULT_LEFT, DEFAULT_TOP);
frame.setFocusableWindowState(false);
frame.setVisible(true);
//define a PrintStream that sends its bytes to the output text area
PrintStream consoleStream = new PrintStream(new OutputStream()
{
public void write (int b) {} //never called
public void write(byte[] b, int off, int len)
{
output.append(new String(b, off, len));
}
});
//set both System.out and System.err to that stream
System.setOut(consoleStream);
System.setErr(consoleStream);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
public static final int DEFAULT_LEFT = 200;
public static final int DEFAULT_TOP = 200;
}
[B]Test.java[/B]
package homenetwork.bkr.training;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ConsoleWindow window = new ConsoleWindow();
[B]window.init();[/B]
}
}
NOTE 1:Core Java Vol I Fundamentals 8th Ed Pg.597 (511).