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

  • Home
  • SEARCH
  • 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 7629721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:54:29+00:00 2026-05-31T05:54:29+00:00

I want to have a JTextArea that can completely work instead of a console

  • 0

I want to have a JTextArea that can completely work instead of a console but I don’t know how to do this!

Thank you

  • 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-31T05:54:31+00:00Added an answer on May 31, 2026 at 5:54 am

    The solution to the problem is to redirect System.{in,out,err} to a JTextArea.


    • Starting with System.out it’s pretty straight forward to to redirect it to your JTextArea component using System.setOut method. In the example below I’ve done this using pipes and SwingWorker but that is all fancy stuff to actually get the output simpler to the swing component.

    • Emulating System.in is simular, you need to redirect your keystrokes to the the System.in using System.setIn. Again, in the example below, I’ve used pipes to get a nicer interface. I also buffer the line (just like a “normal” console would do) till you hit enter. (Note that for example arrow keys will not work but it shouldn’t be to much work to get it also to be handled/ignored.)


    The text in the screenshot below was produced by a number of calls to the “normal” System.out.print.. method and then waiting for input on System.in using a Scanner:

    screenshot

    public static JTextArea console(final InputStream out, final PrintWriter in) {
        final JTextArea area = new JTextArea();
    
        // handle "System.out"
        new SwingWorker<Void, String>() {
            @Override protected Void doInBackground() throws Exception {
                Scanner s = new Scanner(out);
                while (s.hasNextLine()) publish(s.nextLine() + "\n");
                return null;
            }
            @Override protected void process(List<String> chunks) {
                for (String line : chunks) area.append(line);
            }
        }.execute();
    
        // handle "System.in"
        area.addKeyListener(new KeyAdapter() {
            private StringBuffer line = new StringBuffer();
            @Override public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (c == KeyEvent.VK_ENTER) {
                    in.println(line);
                    line.setLength(0); 
                } else if (c == KeyEvent.VK_BACK_SPACE) { 
                    line.setLength(line.length() - 1); 
                } else if (!Character.isISOControl(c)) {
                    line.append(e.getKeyChar());
                }
            }
        });
    
        return area;
    }
    

    And the example main method:

    public static void main(String[] args) throws IOException {
    
        // 1. create the pipes
        PipedInputStream inPipe = new PipedInputStream();
        PipedInputStream outPipe = new PipedInputStream();
    
        // 2. set the System.in and System.out streams
        System.setIn(inPipe);
        System.setOut(new PrintStream(new PipedOutputStream(outPipe), true));
    
        PrintWriter inWriter = new PrintWriter(new PipedOutputStream(inPipe), true);
    
        // 3. create the gui 
        JFrame frame = new JFrame("\"Console\"");
        frame.add(console(outPipe, inWriter));
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    
        // 4. write some output (to JTextArea)
        System.out.println("Hello World!");
        System.out.println("Test");
        System.out.println("Test");
        System.out.println("Test");
    
        // 5. get some input (from JTextArea)
        Scanner s = new Scanner(System.in);
        System.out.printf("got from input: \"%s\"%n", s.nextLine());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a textarea that I might want to disable in certain conditions. I
I run into this problem. I have a textarea which I only want to
I have this simple textarea. When it's not selected, I want it to look
This problem looks trivial, but I can't find the solution. When I create a
I have a textarea tag that has text I want a user to edit.
Hello I have an odd question. I want to know if and how to
I have a large amount of text data that I want to display in
Basically I need to create a textarea that is character limited, but will have
Im making a backup program, and I want everything that i have the program
i want to have a textarea where I can edit html code directly. After

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.