简体   繁体   中英

Java Swing Threading: GUI hangs when logic commands are executed

I'm currently working on a project involving control development of a Parrot AR Drone 2.0 Power Edition.

For this, we're using the YaDrone library ( https://vsis-www.informatik.uni-hamburg.de/oldServer/teaching/projects/yadrone/ ) to control the drone. There has been made a DroneController class around this library for more control. This is essentially hosting all the logic used to the drone.

There has been developed a GUI in Java Swing to show various information about the drone.

The problem we're experiencing is that when the program is executed, aka the drone starts, the GUI opens, but hangs/freezes until all of the commands to the drone has been sent and executed by the drone.

Obviously we'd love for the GUI to update simultaneously while the commands are being sent to the drone.

We've been experimenting with SwingUtilities and stuff like that, with no luck. This is the current main class:

Main Class The whole code for the project can be seen here: https://github.com/tMascagni/CDIO_3/tree/dev (specifically in the dev branch.)

The most important parts of this is the ui package, Main.java and DroneController.java.

If anyone has got any ideas on how to solve this, i'd really appreciate any feedback!

In general, the following approach will work:

   Thread t = new Thread(()->{
      // this code will be executed outside the EDT thread, 
      // and will not make the interface non-responsive
      // ...

      SwingUtilities.invokeLater(()->{
         // and this code will execute once the above code is finished,
         // within the EDT thread, and can report on results
      });
   }).start();

A more streamlined approach, but somewhat more complex to implement, is to use a SwingWorker .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM