Friday, March 4, 2011

create java console inside the panel

How can I create an instance of the Java console inside of a panel?

From stackoverflow
  • Here's a functioning class - you'll have to replace my library function EzTimer with Thread.sleep() and try/catch the InterruptedException.

    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);
    

    Here's the class

    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    
    public class TextAreaOutputStream
    extends OutputStream
    {
    
    // *****************************************************************************
    // INSTANCE PROPERTIES
    // *****************************************************************************
    
    private JTextArea                       textArea;                               // target text area
    private int                             maxLines;                               // maximum lines allowed in text area
    private LinkedList                      lineLengths;                            // length of lines within text area
    private int                             curLength;                              // length of current line
    private byte[]                          oneByte;                                // array for write(int val);
    
    // *****************************************************************************
    // INSTANCE CONSTRUCTORS/INIT/CLOSE/FINALIZE
    // *****************************************************************************
    
    public TextAreaOutputStream(JTextArea ta) {
        this(ta,1000);
        }
    
    public TextAreaOutputStream(JTextArea ta, int ml) {
        if(ml<1) { throw new IoEscape(IoEscape.GENERAL,"Maximum lines of "+ml+" in TextAreaOutputStream constructor is not permitted"); }
        textArea=ta;
        maxLines=ml;
        lineLengths=new LinkedList();
        curLength=0;
        oneByte=new byte[1];
        }
    
    // *****************************************************************************
    // INSTANCE METHODS - ACCESSORS
    // *****************************************************************************
    
    public synchronized void clear() {
        lineLengths=new LinkedList();
        curLength=0;
        textArea.setText("");
        }
    
    /** Get the number of lines this TextArea will hold. */
    public synchronized int getMaximumLines() { return maxLines; }
    
    /** Set the number of lines this TextArea will hold. */
    public synchronized void setMaximumLines(int val) { maxLines=val; }
    
    // *****************************************************************************
    // INSTANCE METHODS
    // *****************************************************************************
    
    public void close() {
        if(textArea!=null) {
            textArea=null;
            lineLengths=null;
            oneByte=null;
            }
        }
    
    public void flush() {
        }
    
    public void write(int val) {
        oneByte[0]=(byte)val;
        write(oneByte,0,1);
        }
    
    public void write(byte[] ba) {
        write(ba,0,ba.length);
        }
    
    public synchronized void write(byte[] ba,int str,int len) {
        try {
            curLength+=len;
            if(bytesEndWith(ba,str,len,LINE_SEP)) {
                lineLengths.addLast(new Integer(curLength));
                curLength=0;
                if(lineLengths.size()>maxLines) {
                    textArea.replaceRange(null,0,((Integer)lineLengths.removeFirst()).intValue());
                    }
                }
            for(int xa=0; xa<10; xa++) {
                try { textArea.append(new String(ba,str,len)); break; }
                catch(Throwable thr) {                                                 // sometimes throws a java.lang.Error: Interrupted attempt to aquire write lock
                    if(xa==9) { thr.printStackTrace(); }
                    else      { EzTimer.delay(200);    }
                    }
                }
            }
        catch(Throwable thr) {
            CharArrayWriter caw=new CharArrayWriter();
            thr.printStackTrace(new PrintWriter(caw,true));
            textArea.append(System.getProperty("line.separator","\n"));
            textArea.append(caw.toString());
            }
        }
    
    private boolean bytesEndWith(byte[] ba, int str, int len, byte[] ew) {
        if(len<LINE_SEP.length) { return false; }
        for(int xa=0,xb=(str+len-LINE_SEP.length); xa<LINE_SEP.length; xa++,xb++) {
            if(LINE_SEP[xa]!=ba[xb]) { return false; }
            }
        return true;
        }
    
    // *****************************************************************************
    // STATIC PROPERTIES
    // *****************************************************************************
    
    static private byte[]                   LINE_SEP=System.getProperty("line.separator","\n").getBytes();
    
    } /* END PUBLIC CLASS */
    
    OscarRyz : Look interesting. Do you have an screenshot?
    Software Monkey : Not much to see... it's a console window! But I see someone has already posted their test program with a pic in another answer.
    OscarRyz : It was me! :) , I couldn't resists.
  • @Sofware Monkey:

    It works!! :)

    alt text

    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 );
    
            for( int i = 0 ; i < 100 ; i++ ) {
                System.out.println( i );
                Thread.sleep( 500 );
            }
        }
    }
    
    furtelwart : Please upvote his answer if it works.
    OscarRyz : If helps for anything. I've mark this as community wiki. At least I won't get points for something I didn't program.
  • what is Ioscape

0 comments:

Post a Comment