Home arrow static arrow Java Programming [Archive] - Simple way to get console style input and output in an applet?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Simple way to get console style input and output in an applet?
This topic has 8 replies on 1 page.

Posts:81
Registered: 6/9/04
Simple way to get console style input and output in an applet?  
Jul 30, 2004 1:00 PM



 
Alot of time when I do school projects its console style input and output. A question that popped into my head was is there a simple way to get console style input and output in an applet window. (for porting to applets for giggles~)

Obviously the traditional graphical UIs are available but is there a simple way to preserve System.in and System.out such that an equivalent operates through the applet window.

Thanks.
 

Posts:3,999
Registered: 00-05-26
Re: Simple way to get console style input and output in an applet?  
Jul 30, 2004 1:04 PM (reply 1 of 8)



 
Well, the java console already gives you System.out results. I've never tried using System.in through it, but i wouldn't be surprised if that works too. Have you tried it?
 

Posts:12,831
Registered: 2/22/00
Re: Simple way to get console style input and output in an applet?  
Jul 30, 2004 1:42 PM (reply 2 of 8)



 
You can remap System.in etc. with java.lang.System.setIn etc.
So you could write a GUI element, or write some Listeners that you use with an existing GUI element, that provides InputStreams and PrintStreams, and then remap the System.* streams to those.

It may be better though, rather than remapping System.* streams, to refactor your command-line programs so that they don't hardcode their I/O to/from the System.* streams.
 

Posts:14,142
Registered: 99-04-02
Re: Simple way to get console style input and output in an applet?  
Jul 30, 2004 1:44 PM (reply 3 of 8)



 
Yeah, you could write a little GUI that has a text field for entry and a text area for output. You redirect System.out to the text area, then push text field text into System.in.
 

Posts:81
Registered: 6/9/04
Re: Simple way to get console style input and output in an applet?  
Jul 30, 2004 3:21 PM (reply 4 of 8)



 
I like overriding system.out system.in. Once the applet is done it should only have to be written once other then changing the Yourclass.main() line. Rewriting every little stupid program to use a GUI involves work for every little stupid program~ instead of one solution for all.

ie
class Console extends Applet{   public void init(){       //override the streams    }    public void start(){      Yourclass.main(stringargshere); //its static after all    }   public void paint(){    } }


Which leaves strategies for pushing streams into a console type view/controller archetype unworked. Seperating them seems quickest. Writing them in one pane seems hard if your thinking about JTextPane. But its something thats the most natural way to port console programs.

The output to a JTextPane is easy. The input however involves having some specific knowledge about how JTextPane's model operates. Quicker might just be painting and taking input yourself. At the moment im investigating JTextPanes models specific ability to output input to a stream. If it can generically output to a stream then that stream can be wrapped and put into system.in. However there is the question of what happens to system.out piped in getting piped out to system.in that depends on specifics.
 

Posts:14,142
Registered: 99-04-02
Re: Simple way to get console style input and output in an applet?  
Jul 30, 2004 3:24 PM (reply 5 of 8)



 
I guess you could add a document listener to the JTextArea's document and for each char, push it into System.in. I think it'd be easier to have a separate input field, and when the user hits enter, the action listener takes the text and pushes it into System.in.
 

Posts:12,831
Registered: 2/22/00
Re: Simple way to get console style input and output in an applet?  
Jul 30, 2004 4:38 PM (reply 6 of 8)



 

Before you do this you may want to write a quick test to confirm that you're allowed to remap System.* streams within applets. It occurs to me that this may be something that the sandbox does not allow.

If your applications were written with a hardcoded assumption that all I/O comes from System.in/out, then arguably they're not so great that they're worth reusing without refactoring.
 

Posts:81
Registered: 6/9/04
Re: Simple way to get console style input and output in an applet?  
Jul 30, 2004 5:04 PM (reply 7 of 8)



 
Computer science experience:
Learn to say "im not sure but its the problems fault" in 250 words or more.
 

Posts:12,831
Registered: 2/22/00
Re: Simple way to get console style input and output in an applet?  
Jul 30, 2004 5:37 PM (reply 8 of 8)



 

What?
 
This topic has 8 replies on 1 page.