Textfield output redirecting

  • Thread starter Olivier Merigon
  • Start date
O

Olivier Merigon

Hi,

I'am looking for a set of class that can be used to redirect standard
output and standard input to a Text field like composant.
Example:
When I do a System.out.println("test"), I want the text to be displyed in a
text field composant instead of bieng displayed in the console.

I am sure a lot of people have already done that? so I was wondering if
somebody can give me a link to find ready to use classes that do the job.

Thanks in advance,


Olivier
 
M

mromarkhan

Peace be unto you
Here is a guess.
Disclaimer: Resources may not be released properly.
<code>
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Redirect_v2 extends JFrame
{
JTextArea jtextarea;
ByteArrayOutputStream byteStream;
ReadSystemOut readThread;
public Redirect_v2() throws IOException
{
byteStream= new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(byteStream);
System.setOut(printStream);
jtextarea = new JTextArea();
jtextarea.setText(byteStream.toString());
this.getContentPane().add(new JScrollPane(jtextarea),BorderLayout.CENTER);
this.setSize(512,342); //hypercard
this.setVisible(true);
readThread = new ReadSystemOut();
readThread.start();
this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
readThread =null;
System.exit(0);
}
}
);
//References:Message-ID: <[email protected]>
//Example:When I do a System.out.println("test"), I want the text to be displyed in a
//text field composant instead of bieng displayed in the console
System.out.println("Peace be unto you");
System.out.println("http://www.hti.umich.edu/cgi/k/koran/koran-idx?type=simple&q1=peace&size=First+100");
}


public static void main(String [] s) throws IOException
{
new Redirect_v2();
}

public class ReadSystemOut extends Thread
{
Runnable doGet = new Runnable()
{
public void run()
{
String text = byteStream.toString();
if(byteStream.size() > 0)
{
jtextarea.append(text);
byteStream.reset();
}
}
};
public void run()
{
Thread myThread = Thread.currentThread();
while (readThread == myThread)
{
SwingUtilities.invokeLater(doGet);
try
{
this.sleep(1000l);
}
catch(InterruptedException ie)
{
}
}
}
}
}
</code>

Haca -- Have a good day.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top