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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:27:55+00:00 2026-05-18T00:27:55+00:00

I have a Swing program where work is continuously being done in a non-Swing

  • 0

I have a Swing program where work is continuously being done in a non-Swing thread. It often needs to update a JTextPane — frequently many times per second. I realize that setText() needs to be called from back inside the event-dispatching thread, but I cant figure out how to make this happen smoothly.

The following minimal complete example is as close as I’ve been able to get it, using a PipedInputStream/PipedOutputStream pair, but this only seems to update the screen once every second or so. I’m not sure what’s taking so long.

import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class TextTest extends JFrame {
    private JTextPane out = new JTextPane();
    private PipedInputStream pIn = new PipedInputStream();
    private PrintWriter pOut;

    public TextTest() {
        try {
            pOut = new PrintWriter(new PipedOutputStream(pIn));
        }
        catch (IOException e) {System.err.println("can't init stream");}

        add(new JScrollPane(out));
        setSize(500, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        // Start a loop to print to the stream continuously
        new Thread() {
            public void run() {
                for (int i = 0; true; i++) {
                    pOut.println(i);
                }
            }
        }.start();

        // Start a timer to display the text in the stream every 10 ms
        new Timer(10, new ActionListener() {
            public void actionPerformed (ActionEvent evt) {
                try {
                    if (pIn.available() > 0) {
                        byte[] buffer = new byte[pIn.available()];
                        pIn.read(buffer);
                        out.setText(out.getText() + new String(buffer));
                    }
                }
                catch (IOException e) {System.err.println("can't read stream");}
            }
        }).start();
    }

    public static void main(String[] args) {
        new TextTest();
    }
}

Am I implementing this wrong? Do I just have totally the wrong idea about how to continuously update a JTextPane from outside the EDT?

  • 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-18T00:27:56+00:00Added an answer on May 18, 2026 at 12:27 am

    The setText() “method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information.”

    Addendum: For reference, here’s some other approaches to updating on the EDT. Another thing to note is that the action event handler for javax.swing.Timer executes on the EDT. Here’s my variation:

    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import javax.swing.text.DefaultCaret;
    
    public class TextTest extends JFrame {
    
        private JTextArea out = new JTextArea();
        private PipedInputStream pIn = new PipedInputStream();
        private PrintWriter pOut;
    
        public TextTest() {
            try {
                pOut = new PrintWriter(new PipedOutputStream(pIn));
            } catch (IOException e) {
                System.err.println("can't init stream");
            }
    
            DefaultCaret caret = (DefaultCaret) out.getCaret();
            caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    
            add(new JScrollPane(out));
            setSize(300, 500);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
    
            // Start a loop to print to the stream continuously
            new Thread() {
    
                public void run() {
                    for (int i = 0; true; i++) {
                        pOut.println(i);
                    }
                }
            }.start();
    
            // Start a timer to display the text in the stream every 10 ms
            new Timer(10, new ActionListener() {
    
                public void actionPerformed(ActionEvent evt) {
                    try {
                        out.append(String.valueOf((char) pIn.read()));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    
        public static void main(String[] args) {
            new TextTest();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newbie to swing development. I have a swing app that needs to
I have a swing gui with a tabbed pane in the north. Several key
I have a swing application that includes radio buttons on a form. I have
I have a Swing Application running under Linux, that has problems to display accented
I have a Java swing application with a panel that contains three JComboBoxe s
I have a Java Swing application, developed on Mac OS X 10.5 using Java
I am creating an touch screen application using Swing and have a request to
I'm relatively new to java (specifically swing) and have recently been making some fairly
I have a small (500kb) swing applet that displays very simple/limited set of small
I have a JApplet which I contains various Swing components. It also starts a

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.