How to update JFrame shortcut in stream? - Java

I tried a lot, but I can not get it to work.

I was told to use EDT with the following example.

SwingUtilities.invokeLater(new Runnable() { public void run() { // Modify the GUI here } }); 

I read a lot on this topic and still do not understand. I understand what a stream is, but .invokeLater still doesn't make any sense to me. Honestly, if you can explain in detail , this will be a big help!

The purpose of the program: To get a randomly generated key, which is constantly created every second, so that later it can be updated in the graphical interface.

+4
source share
5 answers

So there is an EDT (event dispatch stream). All actions that appear on your screen are performed by EDT. There is only one EDT for each swing application.

Are you in some arbitrary stream and want to update QUI through this stream? Well, as I said, there is only one EDT for each swing application, so you have to say that the EDT displays a shortcut (or any other context).

The idea here is that you click this Runnable on the queue from which the EDT is drawn. In the end, your runnable will be processed by EDT when all other actions before it are copied.

+3
source

I recommend that you get the book Filthy Rich Clients . There is a chapter where they explain in detail the Threading Swing model.

Basically in Swing, any code that modifies the GUI must be executed in the event manager thread. The SwingUtilities class that you use there provides you with an easy way to send events to the event queue, which is then dispatched by the EDT. What the invokeLater method invokeLater is it takes the new Runnable() argument, which ultimately runs in the EDT.

From book:

The invokeLater () implementation is executed, the creation caretaker, and the event containing the Runnable. This event is processed on the EDT in the order was received, like any other event. When the time comes, it is sent by running the runnables run () method.

+1
source

If you all want to do this, refresh the user interface in a well-known schedule, try something like the following. This assumes that a JFrame is the component that you want to update every 1 second.

 private static final int WAIT_LENGTH = 1000; // 1 second private JFrame frame = new JFrame(); // Thread will update the UI (via updateUI() call) about every 1 second class UIUpdater extends Thread { @Override void run() { while (true) { try { // Update variables here } catch (Exception e) { System.out.println("Error: " + e); } finally { frame.repaint(); Thread.sleep(WAIT_LENGTH); } } } } 

To start this topic:

 UIUpdater t = new UIUpdater(); t.start(); 
0
source

This is a fairly common GUI programming element. You have one thread that processes the graphical user interface, receives input, and makes callbacks. If another thread tries to modify GUI-related objects, it will conflict with the GUI thread. Say, for example, it was half by drawing something, and you change the color from another thread.

Everything invokeLater makes a queue for something for the GUI thread to run. Later, it works almost instantly, but the current thread does not need to wait for it. The GUI thread may draw or wait for the callback to return, which delays the execution of the code you gave it.

0
source

You need to be a member so we can change it and still use it from the inner class

 protected long secret=0; 

... it should be in your code somewhere it will run ...

 JFrame f = new JFrame("foo"); new Thread(){ public void run() { for(;;){ try { sleep(1000); } catch Interrupted (Exception ix){ return; } // TODO update your secret key here // please don't use random() SwingUtilities.invokeLater(new Runnable() { public void run() { f.setTitle("secret "+x); } }); } } }).start(); 

....

Only update Swing from EDT so that it draws correctly.

When you are in EDT (the code runs in the event handler), you can call paintImmediately () if you really should.

0
source

All Articles