0

In the code below I want to do something after invokeLater is done. 1) I can not use invokeAndWait due to setModel being called by dispatcher. 2) I can not change the value of a final variable inside thread! then how can I wait for involeLater to be done first?

 boolean isDone=false;
 SwingUtilities.invokeLater(new Runnable(){public void run(){
    setModel(new DefaultTableModel(oRecordTable, sIP_TABLE_COL_NAMES_LIST));
    isDone = true; // CAN NOT BE CHANGED HERE, AS HAS TO BE FINAL
 }});
 while (!idDone){
 //wait
 }
 // do something here after the invokeLater is done
C graphics
  • 7,308
  • 19
  • 83
  • 134
  • 2
    Sounds like a poor design issue. Why are you using invokeLater? Code executed in an ActionListener is already invoked on the EDT. Your question needs more clarification because you can't tell when the code is finished unless you set a switch of some kind. – camickr Nov 04 '13 at 18:04

1 Answers1

1

You could do that using a CountDownLatch. BUT this will be exactly the same as using invokeAndWait(), blocking the current Thread, so it does not make any sense.

correct:

SwingUtilities.invokeLater(new Runnable(){public void run(){
    setModel(new DefaultTableModel(oRecordTable, sIP_TABLE_COL_NAMES_LIST));
    isDone = true; // CAN NOT BE CHANGED HERE, AS HAS TO BE FINAL

    // do something here after the invokeLater is done <=====

 }});

CountDownLatch (useless, see above)

final CountDownLatch latch = new CountDownLatch(1);

SwingUtilities.invokeLater(new Runnable(){public void run(){
    setModel(new DefaultTableModel(oRecordTable, sIP_TABLE_COL_NAMES_LIST));
    isDone = true; // CAN NOT BE CHANGED HERE, AS HAS TO BE FINAL

    latch.countDown();

 }});

latch.await(); // could use invokeAndWait as well if blocking thread here ..
R.Moeller
  • 3,436
  • 1
  • 17
  • 12