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 8030115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:39:46+00:00 2026-06-05T00:39:46+00:00

Client has gui and extra thread (to process socket input and print it out

  • 0

Client has gui and extra thread (to process socket input and print it out to passes object of type PrintStream). The gui form has new javax.swing.JTextArea(). I need to pass to thread the object PrintStream to write to: ClientThreadIn(PrintStream inOutput){...}. How to create/bind gui JTextArea to accept data form ClientThreadIn using PrintStream?


Client:

    in = new BufferedReader(new InputStreamReader(s.getInputStream())); 
    out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
ClientThreadIn threadIn = new ClientThreadIn(in, System.out); // client passes it's System.out to thread for writing

So JTextArea should be similar to console. It should be able to accept data from Thread (actually Thread writes to PrintStream of gui)…
Is there something similar to JTextArea.getInputStream()?

  • 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-06-05T00:39:47+00:00Added an answer on June 5, 2026 at 12:39 am

    One way is to create a class that links the JTextArea to an OutputStream, say called TextAreaOutputStream, and have it extend OutputStream. Give it a StringBuilder object to hold the String that it is constructing in the write(int b) override, and give it a reference to the JTextArea that you want to write text to. Then when a new line character is encountered, write the String to the JTextArea, but be sure to do this on the Swing event thread or EDT.

    For example:

    import java.io.IOException;
    import java.io.OutputStream;
    
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    
    public class TextAreaOutputStream extends OutputStream {
    
       private final JTextArea textArea;
       private final StringBuilder sb = new StringBuilder();
       private String title;
    
       public TextAreaOutputStream(final JTextArea textArea, String title) {
          this.textArea = textArea;
          this.title = title;
          sb.append(title + "> ");
       }
    
       @Override
       public void flush() {
       }
    
       @Override
       public void close() {
       }
    
       @Override
       public void write(int b) throws IOException {
    
          if (b == '\r')
             return;
    
          if (b == '\n') {
             final String text = sb.toString() + "\n";
             SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                   textArea.append(text);
                }
             });
             sb.setLength(0);
             sb.append(title + "> ");
    
             return;
          }
    
          sb.append((char) b);
       }
    }
    

    Then it is trivial to wrap this in a PrintStream object and have it used by your socket.

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

Sidebar

Related Questions

The p4v Perforce GUI client has an 'Actions > Remove from Workspace' menu command
My client has recently taken ownership of an existing web site, with final deliverable
My client has a compiled ASP.NET 2.0 application that was compiled & deployed a
A client has given me a spreadsheet of hundreds of domain names. My task
A client has asked for a page loader, something attractive to let the visitor
My client has a website but doesn't check emails often. He has a lot
My client has a asp.net application, using ajax, he has a datagrid that shows
A client has asked me to make their YouTube channel look similar this one:
My client has PHP running on an IIS server, I know these vti_conf files
a client has asked me for a Facebook application similar to a news reader,

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.