How to transfer message from workflow to GUI in java

How to pass message from workflow to GUI in java? I know that in Android this can be achieved with handlers and a message class. But I want the same thing in Java to help me. Thanks in advance. Rangathath.tm

0
source share
5 answers

You should use SwingUtilities.invokeLater because Swing components should only be available from the event dispatch stream.

There is a link to the Swing tutorial on threads in the javadoc of this method. Follow this link.

Here is an example:

 public class SwingWithThread { private JLabel label; // ... public void startBackgroundThread() { Runnable r = new Runnable() { @Override public void run() { try { // simulate some background work Thread.sleep(5000L); } catch (InterruptedException e) { // ignore } // update the label IN THE EDT! SwingUtilities.invokeLater(new Runnable() { @Override public void run() { label.setText("Background thread has stopped"); } }); }; }; new Thread(r).start(); } } 
+3
source

I think the best way to do this is to use EventBus and MVP for your GUI components. A "working thread" fires an event by sending it to the bus, and Presenters, who are handlers for a certain type of event, are notified about this.

A good description of this design can be found here: Is there a recommended way to use the Observer pattern in MVP using GWT?

... although the question is that the GWT answer applies to all applications developed in accordance with MVP.

+2
source

Send events. See this tutorial

+1
source

We do it this way on FrostWire, through this utility function we can check if the runnable / thread used is running from the GUI thread

 /** * InvokesLater if not already in the dispatch thread. */ public static void safeInvokeLater(Runnable runnable) { if (EventQueue.isDispatchThread()) { runnable.run(); } else { SwingUtilities.invokeLater(runnable); } } 
+1
source

You can use the SwingWorker class designed to solve this case: http://download.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

0
source

Source: https://habr.com/ru/post/896119/


All Articles