1

I have a button "Login" in my GUI, when I press it, it goes here:

private void loginBtn(java.awt.event.ActionEvent evt) {
    Login log = new Login(robot, driver, username, password, server, url);
    logStatus = log.login();
    Control check = new Control();
    check.loop();}

Well, I'd like that when I log in, it logs in and then start playing the loop. The loop

check.loop();

is an infinite loop but the GUI stays locked during all the time running it from the action performed of the login button. So how can i call the loop without lock the GUI?

Davide Melis
  • 39
  • 1
  • 8
  • post the loop . – Kaushal28 Jun 26 '17 at 14:06
  • 4
    Well, you should not enter an infinite loop inside an event handler. The event runs on the Event Dispatch Thread. It should only be used for quick operations. Otherwise... well... the GUI freezes. – RealSkeptic Jun 26 '17 at 14:12
  • The `actionPerformed` is already on UI thread. the `check.loop()` should be carried out in a separate non-UI thread. That will relieve the UI. Also an infinite loop ? By having an infinite loop even in a separate thread - it will render a portion of your CPU useless. – SomeDude Jun 26 '17 at 14:12
  • But how can I call the loop after I press the login button if I don't call it inside the event handler? @svasa – Davide Melis Jun 26 '17 at 14:32
  • Have you taken a look at the [Swing concurrency tutorial](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html)? – RealSkeptic Jun 26 '17 at 14:35
  • I'm going to try it, thanks. (This is my first GUI ^^) – Davide Melis Jun 26 '17 at 14:41
  • @DavideMelis Take a tour through the [`SwingWorker`](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/simple.html) and an example also [here](https://en.wikipedia.org/wiki/SwingWorker#Complete_Worker_example)- this is what you need – SomeDude Jun 26 '17 at 15:03
  • Really thank you guys! I was stuck on it for several days – Davide Melis Jun 26 '17 at 15:20

0 Answers0