Textfield output redirecting

Discussion in 'Java' started by Olivier Merigon, May 3, 2004.

  1. 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
     
    Olivier Merigon, May 3, 2004
    #1
    1. Advertising

  2. Olivier Merigon

    Guest

    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: <c752mo$f52$>
    //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.
     
    , May 18, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Aspinall
    Replies:
    0
    Views:
    366
    Paul Aspinall
    Feb 26, 2006
  2. Birch
    Replies:
    3
    Views:
    1,032
    Birch
    Oct 21, 2003
  3. Eddie Parker

    RE: Redirecting system output quietly

    Eddie Parker, Nov 16, 2004, in forum: Python
    Replies:
    4
    Views:
    410
    Donn Cave
    Nov 16, 2004
  4. MrBlueSky
    Replies:
    2
    Views:
    352
    MrBlueSky
    May 10, 2006
  5. Stumped and Confused

    Output VALUE of INPUT textfield using document.write

    Stumped and Confused, Sep 17, 2004, in forum: Javascript
    Replies:
    13
    Views:
    282
    Richard Cornford
    Sep 23, 2004
Loading...

Share This Page