Questions tagged [invokeandwait]

A method provided by the SwingUtilities class in Java that executes synchronously code on the AWT event dispatching thread in order to correctly manipulate UI elements.

SwingUtilities.invokeAndWait is a method provided by the Swing Library in Java in order to execute code on the AWT dispatching thread in a synchronous fashion. As in the majority of graphical toolkits, an arbitrary thread is not allowed to update directly the UI but should asks the UI thread to execute it on its behalf.

Similar constructs exists in other UI toolkits (for example, in .NET there are Invoke/InvokeRequired methods in Windows Forms and the Dispatcher object in WPF).

This tag should be used to emphasize that invokeLater is used in the code, thus ruling out concurrency issues on the UI.

Related tags:

Additional resources:

  • The Java Tutorial has a chapter on invokeLater and invokeAndWait.
25 questions
5
votes
1 answer

Error handling in SwingWorker

My question is a theory-based question, but it does meet a specific need I have. What do you do when your SwingWorker throws an Exception that you a) can anticipate and b) need to recover from and continue, but you want to notify the user that this…
ryvantage
  • 13,064
  • 15
  • 63
  • 112
5
votes
6 answers

Java Swing: main class wait until JFrame is closed

I need some help with a simple java application which makes use of two jframe to get some input parameters. Here's a sketch of my code: //second jframe, called when the button OK of the first frame is clicked public class NewParamJFrame extends…
pips
  • 91
  • 1
  • 1
  • 8
4
votes
3 answers

Java API "Run on EDT if not on EDT"

Just a musing about some repetitive code I have: Runnable run = new Runnable() { @Override public void run() { // Some EDT code } }; if (!EventQueue.isDispatchThread()) { SwingUtilities.invokeAndWait(run); } else { run.run(); } It's…
Whired
  • 259
  • 1
  • 11
4
votes
1 answer

Java happens-before relationship invokeAndWait

My question is related to this question, which already has an answer: yes, there is a happens-before relationship imposed between actions of the thread calling invokeLater/invokeAndWait and actions on the EDT of the runnable thereby submitted. My…
stonar96
  • 1,359
  • 2
  • 11
  • 39
3
votes
4 answers

Returning values from Swing using invokeAndWait

I've been using the following approach to create components and return values from Swing to/from outside the EDT. For instance, the following method could be an extension to JFrame, to create a JPanel and add it to the parent JFrame: public JPanel…
Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
3
votes
3 answers

invokeAndWait Java

Can any one tell me what is the actual difference between this two codes as they both produce same result? code1: public class JLabelDemo extends JApplet { public void init() { this.setSize(400, 400); ImageIcon ii = new…
user2240440
2
votes
4 answers

How does a Java thread synchronize with invokeLater()?

I have a non-GUI thread that starts a JFrame using java.awt.EventQueue.invokeLater(new Runnable() { public void run() { cardReadPunchGUI = new IBM1622GUI(); // instantiate cardReadPunchGUI.setVisible(true); } }); Part of…
Chap
  • 3,649
  • 2
  • 46
  • 84
2
votes
1 answer

Antlr 4 JDialog created by tree.inspect(parser) is automatically closed by JUnit

In Antlr 4, such code works in a general main function. public static void main(String[] args) { ..... SicstusPrologParser parser = new SicstusPrologParser(tokens); ParserRuleContext tree =(ParserRuleContext)parser.program(); …
frankli22586
  • 710
  • 6
  • 19
2
votes
3 answers

invokeAndWait seems to cause the application to freeze intermittently

A process which occurs in the background fires callbacks to ask various questions. In this case the question is "is it okay to migrate your data?", so I have to ask the user. Since we have to do all Swing work on the EDT, this ends up looking like…
Hakanai
  • 12,010
  • 10
  • 62
  • 132
1
vote
3 answers

Waiting for invokeLater() to be called

Is there a more elegant way to do what I'm doing below? That is, is there a more elegant way than polling and sleeping, polling and sleeping, and so on to know when a Runnable.run() method has been called via invokeLater()? private int myMethod()…
Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
1
vote
1 answer

How to implement a loop logic in a Swing application?

My title is somewhat cryptic but I couldn't come up with a clear one. First, two code snippets to establish a point of reference (hopefully I have no typos): Input with Scanner ''' Scanner sc = new Scanner(System.in); for (int i=0; i<10; ++ i) { …
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
1
vote
0 answers

Do I need Suntoolkit.realSync after SwingUtilities.invokeAndWait()?

To be 100 % sure that the java application has refreshed everything on the screen do I need to call SunToolkit.realSync() after SwingUtilities.invokeAndWait() ? I'm creating my GUI inside SwingUtilities.invokeAndWait(). I would like to know if…
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
1
vote
1 answer

Java invokeAndWait?

This is a really simple question about the invokeAndWait thing from swing utilities. I have heard that it synchronizes code execution on a single thread, but I'm not sure. If so, should I use invokeAndWait to do that?
Boogley Beegly
  • 95
  • 1
  • 3
  • 11
0
votes
1 answer

java - cannot assign value in invokeAndWait to global string variable

I am developing an application using the java-simple-serial-connector API to read and write to serial port. I am facing a problem when I try to read data bytes and assign them to a global String variable (sReader). sReader does not get the whole…
jadrijan
  • 1,438
  • 4
  • 31
  • 48
0
votes
0 answers

My method that is supposed to update every frame using a thread won't work

I am trying to make a method that updates every Frame. I am using a thread with an invokeandwait method. My goal is to execute the CashCount method every frame forever. Even though I'm not getting any errors the whole thing just won't work. Please…
HelpMePls
  • 1
  • 1
1
2