0

Hi i have implemente invoke later method in below way but still after i click on the next button i am not able to get response from the Close button. Please point out hte mistake in my code, Or please tell me if the behaviour i am expecting is wrong.

public void actionPerformed(ActionEvent e) {
//Some log messgae  
    if (e.getSource() == btnNext) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){

/// some piece of code for some database transaction

            }
        });

    }

i Expect here that if i click on btnNExt still i should be able to close the application in between when it doing its database transaction. which is not happening?

am i expecting something wrong here or there is some issue with by code.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Katiyman
  • 827
  • 2
  • 12
  • 32
  • invokeLater will execute on the swing event thread, and will freeze the UI while running. It seems like your database transaction code is holding up the UI and should be running on a separate thread. If there is no swing code, do not run it with invokeLater. – schmop May 15 '14 at 13:32
  • 1
    Use SwingWorker instead of invokeLater for your databse code – Sergiy Medvynskyy May 15 '14 at 13:38

1 Answers1

0

What you're expecting is wrong.

SwingUtilities.invokeLater will not start your operation asynchronously, it just starts after the button event handler has been returned (and no other EDT events in the queue) - so basically it starts your code when your app is not busy handling a GUI event.

To do anything asynchronously, you need to use a Thread. Refer to the SwingWorker class as an example. However, I believe your database operation is willing to return some data or do something on the UI (unless you're only deleting something from the db?)

That opens up another set of problems, since you shall never update anything on the GUI from another thread. There you might call SwingUtilities.invokeAndWait from the db thread to update something on the GUI.

Gee Bee
  • 1,794
  • 15
  • 17