Updating one JTextPane out of many different classes

aProgger

I have to synchronise two databases via a button. Under the Button I have a JTextPane in a JScrollPanel. My TextPane class looks like:

public class InfoArea extends JTextPane {
  private final HTMLEditorKit   kit              = new HTMLEditorKit();
  private final HTMLDocument    doc              = new HTMLDocument();
  private static ia;

  public static InfoArea getInfoArea {
    if (ia == null) ia = new InfoArea();
    return ia;
  }

  private InfoArea() {
    setEditorKit(kit);
    setDocument(doc);

    showInfo("Please wait...");

    setEditable(false);
  }

  public void showInfo(String msg) {

    try {
      kit.insertHTML(doc, doc.getLength(), "<b><font color=\"#00AA00\">" + msg + "</font></b>", 0, 0, null);
    } catch (BadLocationException | IOException e) {
      e.printStackTrace();
    }
  }
}

The "TextPane" is added to SOUTH in the GUI.

Now I have a few classes which do:

private static final InfoArea ia = InfoArea.getInfoArea();

First I initialise the database connections. Because this is done very fast, the infos are shown at once. But then I do something like this:

private void doSmth() {
  for(int i = 0; i < smth; ++i) {
    if(sync()) ia.showInfo("OK");
    else ia.showInfo("Not OK");
  }
}

In other classes it could be a while on a resultset. The Problem is, the textpane is not updated until every method runs trough. I red about this behaviour and I know the problem is with the Event Dispatcher Thread (EDT). I tried to update the showInfo(String msg) as following:

private void showInfo(String msg) {
  SwingUtilities.invokeLater(new Runnable() {

    public void run() {

      try {
          kit.insertHTML(doc, doc.getLength(), msg, 0, 0, null);
      } catch (BadLocationException | IOException e) {
          e.printStackTrace();
      }
    }
  });
}

But this does not do the trick. I also tried SwingWorker with overriding doInBackground(), publish() and process() but also no success. I also tried a worker thread instead of SwingUitilities. Last I tried:

private void doSmth() {
  for(int i = 0; i < smth; ++i) {
    SwingUtilities.invokeLater(new Runnable() {

      public void run() {
        if(sync()) ia.showInfo("OK");
        else ia.showInfo("Not OK");
      }
    });
  }
}

No success. Also I believe this try is the worst, because it blocks the EDT.

As you might notice, I am a bit desperate at the moment because I do not know where to look else. And sorry for not posting all the tries with SwingWorker and WorkerThreads, but they would blow up this post. And it is large enough already.

Can someone tell me, what I have to do to get it working? I need it for a customer who want see something while the synchronisation process is running. I am also willing to provide more info, if something is missing.

aProgger

Solved it myself. I misunderstood some articles I red yesterday. The solution is, to put the whole task in a thread, not just the method which outputs.

As I said, I click a button to start the synchronisation process. The button calls just a method:

public void syncDB() {

  ia.showInfo("Sync cat...");
  doCatSync();

  ia.showInfo("Sync item...");
  doItemSync();
  ...
}

Now I have to wrap a thread around the tasks:

public void syncDB() {

  new Thread() {

    public void run() {

      ia.showInfo("Sync cat...");
      doCatSync();

      ia.showInfo("Sync item...");
      doItemSync();
      ...
    }
  }.start();
}

That did the trick. I am always open for improvements.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Calling the same function in many different classes

分類Dev

Debug one out of many azure functions

分類Dev

Click one random element out of many

分類Dev

Select elements with different classes (Not one element with multiple classes)

分類Dev

How many singleton classes can we use in one android app?

分類Dev

Opendaylight : One of the rest calls out of many returing 401 error everytime

分類Dev

Testing one Spring Test Class for many different set of profiles

分類Dev

How to use a generic with two different classes but one of them from the VCL

分類Dev

displaying different classes in a table from one jsp file

分類Dev

How would I figure out many Groups have Customers with different countries

分類Dev

Updating CollectionView in a different thread

分類Dev

Making sure a transaction commits before another one starts when using different classes in Seam

分類Dev

Laravel One to Many of One to Many relationship

分類Dev

Mongoose with NestJS - One to Many

分類Dev

Database Query - One to Many

分類Dev

Bidirectional Many to One Mapping

分類Dev

One to many on single row

分類Dev

Laravel Eloquent one to many

分類Dev

SQL Select One to Many

分類Dev

SQLAlchemy many-to-many relationship updating association object with extra column

分類Dev

updating totals with one sql query

分類Dev

ES6 Classes - Updating Static Properties

分類Dev

ObservableObject text not updating even with objectWillChange - multiple classes

分類Dev

One to many and one to one relationship using hibernate

分類Dev

System.outをJTextPaneにリダイレクトする

分類Dev

odd one out algorithm

分類Dev

pyplot is creating many different lines

分類Dev

MVC: Passage of the methods in different classes

分類Dev

collection_select with one-to-many / many-to-many

Related 関連記事

  1. 1

    Calling the same function in many different classes

  2. 2

    Debug one out of many azure functions

  3. 3

    Click one random element out of many

  4. 4

    Select elements with different classes (Not one element with multiple classes)

  5. 5

    How many singleton classes can we use in one android app?

  6. 6

    Opendaylight : One of the rest calls out of many returing 401 error everytime

  7. 7

    Testing one Spring Test Class for many different set of profiles

  8. 8

    How to use a generic with two different classes but one of them from the VCL

  9. 9

    displaying different classes in a table from one jsp file

  10. 10

    How would I figure out many Groups have Customers with different countries

  11. 11

    Updating CollectionView in a different thread

  12. 12

    Making sure a transaction commits before another one starts when using different classes in Seam

  13. 13

    Laravel One to Many of One to Many relationship

  14. 14

    Mongoose with NestJS - One to Many

  15. 15

    Database Query - One to Many

  16. 16

    Bidirectional Many to One Mapping

  17. 17

    One to many on single row

  18. 18

    Laravel Eloquent one to many

  19. 19

    SQL Select One to Many

  20. 20

    SQLAlchemy many-to-many relationship updating association object with extra column

  21. 21

    updating totals with one sql query

  22. 22

    ES6 Classes - Updating Static Properties

  23. 23

    ObservableObject text not updating even with objectWillChange - multiple classes

  24. 24

    One to many and one to one relationship using hibernate

  25. 25

    System.outをJTextPaneにリダイレクトする

  26. 26

    odd one out algorithm

  27. 27

    pyplot is creating many different lines

  28. 28

    MVC: Passage of the methods in different classes

  29. 29

    collection_select with one-to-many / many-to-many

ホットタグ

アーカイブ