Hi,
Im having a problem displaying text in JTextPane. I want to be able to append text (e.g. "Task 1 completed..") after a task is completed. But mine display the text once all the tasks are completed which is quite useless as user does not know what is going on. I know this has something to do with thread n stuff.. but im not quite sure.. any help is greatly appreciated..

Cheers

Dani AI

Generated

The behavior you saw is the standard Swing threading problem: updates to a JTextPane are painted only when the Event Dispatch Thread (EDT) is free. In 's flow the code inserts text and then immediately runs a long task on the same thread (the delete/processing call). Because the EDT is blocked by that work, the pane cannot repaint until the whole sequence finishes.

Do the work off the EDT and send UI updates back to the EDT. The usual pattern is to run long tasks in a background thread (SwingWorker is the built-in helper) and publish progress messages; the worker's process(...) runs on the EDT so it can safely append to the JTextPane. suggestion using getText/setText will work only if done on the EDT and it is inefficient for large documents (it copies the whole text each time). was right to request code — the change is mostly structural: keep UI updates on the EDT and heavy work off it.

Example pattern (minimal) — run tasks in doInBackground(), call publish("Task A done"), and append messages in process(...):

new SwingWorker<Void,String>() {
    protected Void doInBackground() {
        // doTaskA() is long-running
        doTaskA();
        publish("Task A completed");
        doTaskB();
        publish("Task B completed");
        return null;
    }
    protected void process(java.util.List<String> chunks) {
        for (String msg : chunks) {
            try {
                javax.swing.text.Document doc = textPane.getDocument();
                doc.insertString(doc.getLength(), msg + "\n", null);
            } catch (javax.swing.text.BadLocationException e) {
                // log or handle
            }
        }
    }
}.execute();

Quick troubleshooting: verify code that blocks the UI is inside doInBackground(); avoid calling Swing methods from background threads; use done() for final UI work; catch and surface exceptions from the worker. For background and EDT rules see the official Java Swing concurrency guide and the SwingWorker API: Concurrency in Swing and SwingWorker (Java SE 8).

Recommended Answers

All 3 Replies

It would help if you could post some of the source code your using. Can then maybe evaluate the logic a bit more.

here is the partial section of my code

styledDoc.insertString(styledDoc.getLength(), "Removing previous file..", highlight);
        removeData.deleteOldFile(fileDirectory);

        styledDoc.insertString(styledDoc.getLength(), "\n", defaultformat);

         styledDoc.insertString(styledDoc.getLength(), "Time taken: "+(System.currentTimeMillis()-startTime)+" miliseconds.", medium);

Basically I want to append text message to my JTextPane object every time a task is completed. But all messages got displayed after the last task is executed. I want after task A is completed, then append "Task A is completed.." then do task B, prints "Task B is completed"... and so on.. I explicitly put the code in sequence as you can see from the attached code here... I read somewhere about this problem. To solve one need to use Thread. But im not sure how..

Hm you could try the following

String text;
text = textPane.getText();
text += "\nAppended text."
textPane.setText(text);

But i think that there is a better way to do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.