Create Java console inside a GUI panel
Solution 1:
Here's a functioning class. You can install an instance of this into the system out and err using:
PrintStream con=new PrintStream(new TextAreaOutputStream(...));
System.setOut(con);
System.setErr(con);
Updated 2014-02-19: To use EventQueue.invokeLater() to avoid GUI threading issues which can crop up very rarely with the original.
Updated 2014-02-27: Better implementation
Updated 2014-03-25: Correct recording & deletion of lines in text area to be within the run()
method to avoid race-condition between appending and deleting which can happen if the console is flooded with output. The end result seems cleaner to me, as well.
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class TextAreaOutputStream
extends OutputStream
{
// *************************************************************************************************
// INSTANCE MEMBERS
// *************************************************************************************************
private byte[] oneByte; // array for write(int val);
private Appender appender; // most recent action
public TextAreaOutputStream(JTextArea txtara) {
this(txtara,1000);
}
public TextAreaOutputStream(JTextArea txtara, int maxlin) {
if(maxlin<1) { throw new IllegalArgumentException("TextAreaOutputStream maximum lines must be positive (value="+maxlin+")"); }
oneByte=new byte[1];
appender=new Appender(txtara,maxlin);
}
/** Clear the current console text area. */
public synchronized void clear() {
if(appender!=null) { appender.clear(); }
}
public synchronized void close() {
appender=null;
}
public synchronized void flush() {
}
public synchronized void write(int val) {
oneByte[0]=(byte)val;
write(oneByte,0,1);
}
public synchronized void write(byte[] ba) {
write(ba,0,ba.length);
}
public synchronized void write(byte[] ba,int str,int len) {
if(appender!=null) { appender.append(bytesToString(ba,str,len)); }
}
@edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_DEFAULT_ENCODING")
static private String bytesToString(byte[] ba, int str, int len) {
try { return new String(ba,str,len,"UTF-8"); } catch(UnsupportedEncodingException thr) { return new String(ba,str,len); } // all JVMs are required to support UTF-8
}
// *************************************************************************************************
// STATIC MEMBERS
// *************************************************************************************************
static class Appender
implements Runnable
{
private final JTextArea textArea;
private final int maxLines; // maximum lines allowed in text area
private final LinkedList<Integer> lengths; // length of lines within text area
private final List<String> values; // values waiting to be appended
private int curLength; // length of current line
private boolean clear;
private boolean queue;
Appender(JTextArea txtara, int maxlin) {
textArea =txtara;
maxLines =maxlin;
lengths =new LinkedList<Integer>();
values =new ArrayList<String>();
curLength=0;
clear =false;
queue =true;
}
synchronized void append(String val) {
values.add(val);
if(queue) { queue=false; EventQueue.invokeLater(this); }
}
synchronized void clear() {
clear=true;
curLength=0;
lengths.clear();
values.clear();
if(queue) { queue=false; EventQueue.invokeLater(this); }
}
// MUST BE THE ONLY METHOD THAT TOUCHES textArea!
public synchronized void run() {
if(clear) { textArea.setText(""); }
for(String val: values) {
curLength+=val.length();
if(val.endsWith(EOL1) || val.endsWith(EOL2)) {
if(lengths.size()>=maxLines) { textArea.replaceRange("",0,lengths.removeFirst()); }
lengths.addLast(curLength);
curLength=0;
}
textArea.append(val);
}
values.clear();
clear =false;
queue =true;
}
static private final String EOL1="\n";
static private final String EOL2=System.getProperty("line.separator",EOL1);
}
} /* END PUBLIC CLASS */
And here's a screenshot of it in action:
Solution 2:
@Sofware Monkey:
It works! :)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Main{
public static void main( String [] args ) throws InterruptedException {
JFrame frame = new JFrame();
frame.add( new JLabel(" Outout" ), BorderLayout.NORTH );
JTextArea ta = new JTextArea();
TextAreaOutputStream taos = new TextAreaOutputStream( ta, 60 );
PrintStream ps = new PrintStream( taos );
System.setOut( ps );
System.setErr( ps );
frame.add( new JScrollPane( ta ) );
frame.pack();
frame.setVisible( true );
frame.setSize(800,600);
for( int i = 0 ; i < 100 ; i++ ) {
System.out.println( i );
Thread.sleep( 500 );
}
}
}
Solution 3:
I know that this is an old thread but the fact that I found it while trying to figure out a good way of doing this means others probably will too.
Here's a (Probably) cleaner way of doing what software monkey posted:
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import javax.swing.JTextArea;
/**
* Represents a console viewable through a <code>JTextArea</code>.
*
* <p>
* Implementation:
* <code>
* System.setOut(new PrintStream(new Console( ... )));
* </code>
* </p>
*
* @author Derive McNeill
*
*/
public class Console extends OutputStream {
/**
* Represents the data written to the stream.
*/
ArrayList <Byte> data = new ArrayList <Byte> ();
/**
* Represents the text area that will be showing the written data.
*/
private JTextArea output;
/**
* Creates a console context.
* @param output
* The text area to output the consoles text.
*/
public Console(JTextArea output) {
this.output = output;
}
/**
* Called when data has been written to the console.
*/
private void fireDataWritten() {
// First we loop through our written data counting the lines.
int lines = 0;
for (int i = 0; i < data.size(); i++) {
byte b = data.get(i);
// Specifically we look for 10 which represents "\n".
if (b == 10) {
lines++;
}
// If the line count exceeds 250 we remove older lines.
if (lines >= 250) {
data = (ArrayList<Byte>) data.subList(i, data.size());
}
}
// We then create a string builder to append our text data.
StringBuilder bldr = new StringBuilder();
// We loop through the text data appending it to the string builder.
for (byte b : data) {
bldr.append((char) b);
}
// Finally we set the outputs text to our built string.
output.setText(bldr.toString());
}
@Override
public void write(int i) throws IOException {
// Append the piece of data to our array of data.
data.add((byte) i);
// Indicate that data has just been written.
fireDataWritten();
}
}