1
      boolean flag = true;
      public void run(){
      // some code
         loginButton.addActionListener(new ActionListener() { 
         public void actionPerformed(ActionEvent e) {
             flag = false; 
             // Some code
         }
      }); 

      while(flag){}
      }

I am using while loop till the actionListener is invoked which I think is silly perhaps. Please let me know if this can be done in more efficient way.

Akash
  • 4,412
  • 4
  • 30
  • 48

1 Answers1

0

You dont need a Thread for the Listener itself, the Java UI Thread calls your Listener for you.

However if you want your response to the event non-blocking do the following:

// no need for a second thread here
loginButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) {
         // If you dont want to block the UI Thread make a new Thread here
         new Thread(YOUR_RUNNABLE).start();
     }
}

Related Question

Community
  • 1
  • 1
Nick D
  • 1,483
  • 1
  • 13
  • 22