0

I have this JLabel with a text, that I want to move with a KeyListener. When I press the arrow keys, the label should move to the given direction.

The weird thing is, it only works on time, after that I get a very long exception:

"Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Unknown Source)
    at Collatz$2.keyPressed(Collatz.java:64)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Window.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Here is a part of my code, has someone an idea? I'm not really into that thread stuff.. so maybe my attempt is just wrong?

    field = new JLabel("Test");
    add(field);

    Thread t = new Thread(new Runnable(){
        @Override
        public void run(){                        
            System.out.println("In Thread");
            field.setBounds(x_pos,pos_y,50,50);                
        }
    });

    addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) { }

        @Override
        public void keyPressed(KeyEvent e) {  
            // System.out.println("Code: " + e.getKeyCode());

            if (e.getKeyCode() == 27){
                System.exit(0);
            }

            if (e.getKeyCode() == 37){
                pos_x--;
                t.start();
            }

            if (e.getKeyCode() == 38){
                pos_y--;
                t.start();
            }

            if (e.getKeyCode() == 39){
                pos_x++;
                t.start();
            }

            if (e.getKeyCode() == 40){
                pos_y++;
                t.start();
            }                

        }

        @Override
        public void keyReleased(KeyEvent e) {}
    });

    setVisible(true);  
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Sharivari
  • 103
  • 9
  • 2
    A thread can only be started once. I have no idea why you start a thread in the first place. And using swing components from a thread other than the EDT is not safe anyway. Just call field.setBound() from your keyPressed() method. – JB Nizet Jun 16 '16 at 16:02
  • Ignore the duplicate and see http://stackoverflow.com/q/7217013/476716 for what you actually should be doing to update UI asynchronously. – OrangeDog Jun 16 '16 at 16:04
  • Use a Swing Timer and Key Bindings to do this. [For example](http://stackoverflow.com/a/23599688/522444) – Hovercraft Full Of Eels Jun 16 '16 at 16:16

0 Answers0