Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to reassign STDIN to a JTextArea??  RSS feed

 
Gary Sase
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know of a way to redirect stdin to
a textarea??
I want to be able to enter text in the textarea
and have that be my STDIN.
Any help would be appreciated along with any
code samples.
Gary
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary,
We'd like to welcome you to the JavaRanch, but please help us - and yourself out by choosing the right forum to ask your questions. You're asking an AWT/Swing question in the J2EE forum. You'll find more people who know the answers to this and many other GUI-related questions in the Swing/JFC/AWT forum.
A quick answer though is "???". STDIN is a place things come FROM, not a place things go TO. I'll assume you want to be able to do something on the order of directing a text file's contents into a GUI input control, but it would be a good idea if you could give a short example.
 
Gary Sase
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I start up my Java app, I want to capture
keyboard input. The stdin by default is reading
from the terminal window that I started my app in.
I want to change where the keyboard input is
coming from. I have a JTextArea in my app and
that is where I want the keyboard input to come
from. I know I have to capture the keyboard events, but how do I transition from the terminal
window to the JTextArea using System.setIn()??
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this thread to the Swing forum...
Simon
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't have to "redirect" anything. Just type in the JTextArea, and on some event, like pushing a button, use the getText() method of your JTextArea to place the text into a String.
Here is an example

Hope that helps.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid I'm still unclear on the idea here. STDIN isn't the keyboard, it's a data stream. So If you literally mean that when your app starts up you want whatever's typed to appear in the text input area, that's just a matter of setting what's know as the input focus and the method of doing that is by invoking requestFocus() on the input area.
Now, on the other hand, if you have stuff that was typed into the console window before the Java app started and you want to scoop those items up and make a copy into that text window, that's a little stickier!
 
Gary Sase
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this would explain it better.
I am working in JBuilder 8. JBuilder8 IDE contains a message window where the STDOUT, STDERR, and STDIN are assigned or redirected.
The message window is a JTextArea.
When I enter text in the message window, I can
read from stdin and write the entered text to
stdout:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
while((s = br.readLine()).length() != 0)
System.out.println("The text entered was: " + s);
I want to do the same thing that Borland did but
I have my own JTextArea in my application that I
want to set up as STDOUT, STDERR, and STDIN.
It is similar to a Console application. All of
the Console application examples that I have found
online shows how to redirect STDOUT and STDERR to
a textarea but they don't show how to redirect
STDIN to come from a textarea.
I hope this helps.
Gary
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now it begins to make more sense!
Well, a textarea's a container, not a stream, so what you really need to do is set up a process that detects stuff as it's typed into the textarea and stuffs it into a pipeline whose other end has been made the STDIN of the receiving program.
A good start I think would be to see how a conversational program such as a chat window program reads and displays text. Normally, you'd be watching for the ENTER key to be hit as a cue to send another batch of input down the pipe, but looking at some existing code will help you get the scrolling and other aspects right.
I'm afraid I don't have any such code myself, but an Internet search should turn up an example or 6. Chats are very popular Java projects.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!