Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7446941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:28:18+00:00 2026-05-29T12:28:18+00:00

Hey guys I am making a terminal application using Swing and Apache Commons. I

  • 0

Hey guys I am making a terminal application using Swing and Apache Commons. I was able to redirect System.out and System.err to a JTextArea easily but how do I do that for System.in ? Will I need to override Inputstream methods? Do I need to convert the String from JTextArea to byte array and then pass it to InputStream? Code examples would be nice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:28:19+00:00Added an answer on May 29, 2026 at 12:28 pm

    I recently tried the same thing, here’s my code:

    class TexfFieldStreamer extends InputStream implements ActionListener {
    
        private JTextField tf;
        private String str = null;
        private int pos = 0;
    
        public TexfFieldStreamer(JTextField jtf) {
            tf = jtf;
        }
    
        //gets triggered everytime that "Enter" is pressed on the textfield
        @Override
        public void actionPerformed(ActionEvent e) {
            str = tf.getText() + "\n";
            pos = 0;
            tf.setText("");
            synchronized (this) {
                //maybe this should only notify() as multiple threads may
                //be waiting for input and they would now race for input
                this.notifyAll();
            }
        }
    
        @Override
        public int read() {
            //test if the available input has reached its end
            //and the EOS should be returned 
            if(str != null && pos == str.length()){
                str =null;
                //this is supposed to return -1 on "end of stream"
                //but I'm having a hard time locating the constant
                return java.io.StreamTokenizer.TT_EOF;
            }
            //no input available, block until more is available because that's
            //the behavior specified in the Javadocs
            while (str == null || pos >= str.length()) {
                try {
                    //according to the docs read() should block until new input is available
                    synchronized (this) {
                        this.wait();
                    }
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            //read an additional character, return it and increment the index
            return str.charAt(pos++);
        }
    }
    

    and use it like this:

        JTextField tf = new JTextField();
        TextFieldStreamer ts = new TextFieldStreamer(tf);
        //maybe this next line should be done in the TextFieldStreamer ctor
        //but that would cause a "leak a this from the ctor" warning
        tf.addActionListener(ts);
    
        System.setIn(ts);
    

    Has been a while since I coded Java so I may not be up-to-date with the patterns. You should probably also overload int available() but my example just consists of the bare minimum to make it work with a BufferedReaders readLine() function.

    Edit: For this to work for a JTextField you have to use implements KeyListener instead of implements ActionListener and then use addKeyListener(...) on your TextArea. The function that you need instead of actionPerformed(...) is public void keyPressed(KeyEvent e) and then you have to test for if (e.getKeyCode() == e.VK_ENTER) and instead of using the whole text you just use a substring from the last line before the cursor with

    //ignores the special case of an empty line
    //so a test for \n before the Caret or the Caret still being at 0 is necessary
    int endpos = tf.getCaret().getMark();
    int startpos = tf.getText().substring(0, endpos-1).lastIndexOf('\n')+1;
    

    for the input string. Because otherwise you would read the whole TextArea every time you press enter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, I am making an application using C# in Visual Studio that minifies
Hey guys, i am making an android application where i want to show a
Hey guys, I'm using Twitter's PHP API, called twitterlibphp , and it works well,
Hey guys i am trying to automate my system at work for sending artwork
Hey guys i'm making a website but for some reason my div tags are
Hey guys I have a problem with an app I'm making. The thing is
Hey guys, I'm making a quiz with a limit, I want to keep track
Hey guys, first off all sorry, i can't login using my yahoo provider. anyways
Hey guys i'm making a website where you submit a server for advertising. When
Hey guys i'm making my first game in c++ and I was wondering how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.