1

I try to move a JLabel from Position A to Position B sequential in same X and Y Steps.. The Method works correctly, but doesnt't update my gui.. I Show you my code in the following:

When I don't call the .join() Method, the code works, but don't wait with execution on my another Thread... I need that this function don't call when it actually runs.. Can anybody help me?

Thread MoveThread = new Thread( new Runnable() 
    {

        @Override
        public void run() 
        {
            for (int i = 0; i<Const.SteinVerschiebenSchritte; i++) 
            {
                try
                {
                    p_lblToMove.setLocation(p_lblToMove.getLocation().x + x_schritt, p_lblToMove.getLocation().y + y_schritt);
                    System.out.println("SetLoc");
                    Thread.sleep(10);
                }
                catch (InterruptedException ex)
                {
                    StatusLog("InterruptedException");
                }
            }
            System.out.println("invokeLater");


            p_lblToMove.setLocation(p_lblToMove.getLocation().x + x_offset, p_lblToMove.getLocation().y + y_offset);
        }
    });



    MoveThread.start();
    try {
        System.out.println("BeforeJoin");
        MoveThread.join();
        System.out.println("AfterJoin");
        System.out.println("------------------");
    } catch (InterruptedException ex) {
        Logger.getLogger(Spielfeld.class.getName()).log(Level.SEVERE, null, ex);
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
maabaa
  • 11
  • 1

1 Answers1

2

Swing is a single thread environment, meaning that you should not performing any long running or blocking operations from within the context of the Event Dispatching Thread, equally, you Swing is also not thread safe, meaning that you should only update the UI from within the context of the Event Dispatching Thread.

See Concurrency in Swing for more details.

Depending on what you're trying to achieve you could use a SwingWorker, see Worker Threads and SwingWorker for more details, or a Swing Timer, see How to use Swing Timers for more details

Also remember, Swing relies on layout managers to do much of the core work when it comes to positioning and sizing components, you might find that you are fighting the layout mangers which could cause unexpected results

Of course, you could always have a look at The Universal Tween Engine and Sliding layout or Java: Moving jLabel twice using Timer

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I tried using SwingUtilities but it doesn't work.. Is it possible that MoveThread.join(); ist wrong in this context? – maabaa Oct 03 '15 at 09:01
  • *"Is it possible that MoveThread.join(); ist wrong in this context?*" - Most likely. And I didn't say to use `SwingUtilities`, I said to either use a `SwingWorker` or Swing `Timer` (or better yet, a third party library which does what you want it to) – MadProgrammer Oct 03 '15 at 09:25