Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Simple Swing stdin/stdout console for command-line programs

Reply
Thread Tools

Simple Swing stdin/stdout console for command-line programs

 
 
yay_frogs@yahoo.com
Guest
Posts: n/a
 
      01-20-2007
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);
}
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Swing is dead! Long live Swing. Knute Johnson Java 32 02-29-2012 05:10 PM
Why not using javax.swing.event with swing? S.T Java 2 05-25-2007 12:10 AM
javax.swing.Popup, javax.swing.PopupFactory lizard Java 0 01-30-2006 09:34 PM
Swing Model Classes Updating Swing Components on a Thread Other Than AWT mkrause Java 0 05-06-2005 04:32 PM
Java 1.2 Swing vs. Java 1.5 Swing Big Daddy Java 2 04-16-2005 01:14 PM



Advertisments