Simple Swing stdin/stdout console for command-line programs

Discussion in 'Java' started by yay_frogs@yahoo.com, Jan 20, 2007.

  1. Guest

    I'd like to be able to run simple command-line programs that only use
    standard input and output within Swing (or even AWT) so that
    command-line programs can be run in either an applet or via Java Web
    Start. I found online a simple way to redirect standard output to a
    Swing JTextArea:

    http://www.codecomments.com/message188868.html

    However, this doesn't provide a way to redirect text typed into the
    JTextArea to be redirected to standard input, which seems to be a
    trickier problem and more work.

    So before I reinvent the wheel and spend time figuring out the easiest
    way to get characters typed in a JTextArea redirected to standard
    input, has anyone done this before? (It seems like the sort of thing
    someone must have done before.)

    If no one knows of any existing implementation, then I'll probably
    start by trying to go with the same approach used with the link above.
    Here's code from the link above which shows how to define and then use
    a DocumentOutputStream class which extends OutputStream:

    /*
    // Use the below DocumentOutputStream class like this:


    private PrintStream redirectOutput(final JTextArea textarea) {
    Document doc = textarea.getDocument();
    // make the text area scroll automatically
    doc.addDocumentListener(new DocumentListener() {
    public void changedUpdate(DocumentEvent e) {
    textarea.setCaretPosition(e.getOffset());
    }
    public void insertUpdate(DocumentEvent e) {
    textarea.setCaretPosition(e.getOffset());
    }
    public void removeUpdate(DocumentEvent e) {}
    });
    return new PrintStream(new DocumentOutputStream(doc));
    }

    */

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;

    import java.util.*;


    public class DocumentOutputStream extends OutputStream {

    private Document _doc;

    /** Creates a new instance of DocumentOutputStream */
    public DocumentOutputStream(Document doc) {
    if (doc == null) {
    throw new NullPointerException("Cannot pass a null document to this
    constructor");
    }
    _doc = doc;
    }

    public void write(byte[] buf, int offset, int length)
    throws IOException {
    try {
    _doc.insertString(_doc.getLength(),
    new String(buf, offset, length),
    null);
    } catch (BadLocationException ble) {
    throw new IOException("Document append failed : " + ble);
    }
    }

    public void write(int b) throws IOException {
    try {
    _doc.insertString(_doc.getLength(),
    new String(new byte[] {(byte) b}),
    null);
    } catch (BadLocationException ble) {
    throw new IOException("Document append failed : " + ble);
    }
    }
    }
     
    , Jan 20, 2007
    #1
    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. Bernard
    Replies:
    4
    Views:
    5,379
    Raymond DeCampo
    Jan 3, 2006
  2. Bernd Oninger
    Replies:
    1
    Views:
    793
    Martin Honnen
    Jul 7, 2004
  3. Harayasu
    Replies:
    3
    Views:
    594
    moongateclimber
    Aug 21, 2003
  4. GerShar
    Replies:
    1
    Views:
    723
    Dennis Lee Bieber
    Oct 31, 2005
  5. Andrzej Adam Filip
    Replies:
    2
    Views:
    147
    Andrzej Adam Filip
    Feb 22, 2009
Loading...

Share This Page