Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Current Thread not Owner error when using wait()

843806May 31 2007 — edited Jun 2 2007
i posted this at the end of my last post, but that was a different problem so I assumed it was ok to start a new topic.

If i minimize the error message, the program still works, except that i can't access the JMenu, and the error keeps writing in the window each time a key is pressed (wait() is for a keylistener). I have tried using synchronized(this) with wait(), and with both wait() and notify() or notifyAll(), no combination seems to work. When i used synchronized(this), the JFrame showing my arrow buttons appears blank. (it's a game, and the arrow buttons are used (or arrow keys) to move around the game board)
EDIT:
I moved the try catch to the original class, and added a synchronized (new Levels ()) (name of class keylistener is in)
{
wait();
}
The arrows appeared and the keylistener worked, but the error message still appeared.



Here's the error message, followed by a portion of my code, hopefully someone knows what to do. After waiting, the paint method in my original class is repainted.

java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Unknown Source)
at Levels.LevelOne(Levels.java:582)
at User_Interface.paint(User_Interface.java:182)
at javax.swing.JFrame.update(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(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.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)
//***Levels class in which the wait() command is executed, waiting for a key
//to be pressed
up = new JButton (new ImageIcon ("arrow_up.gif"));
        left = new JButton (new ImageIcon ("arrow_left.gif"));
        right = new JButton (new ImageIcon ("arrow_right.gif"));
        down = new JButton (new ImageIcon ("arrow_down.gif"));

        f.getContentPane ().setLayout (new BorderLayout ());
        f.getContentPane ().add (up, BorderLayout.NORTH);
        f.getContentPane ().add (left, BorderLayout.WEST);
        f.getContentPane ().add (right, BorderLayout.EAST);
        f.getContentPane ().add (down, BorderLayout.SOUTH);
        f.pack ();

        f.setLocation (600, 350);
        f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        f.setVisible (true);
        KeyHandler key = new KeyHandler ();
        up.addKeyListener (key);
        left.addKeyListener (key);
        right.addKeyListener (key);
        down.addKeyListener (key);








        up.addActionListener (new ActionListener ()
        {
            public void actionPerformed (ActionEvent e)
            {

                yCord -= 1;
                yLoc += 40;
                label = "LevelOne";
                ok = true;
                f.setVisible (false);
            }
        }
        );
        left.addActionListener (new ActionListener ()
        {
            public void actionPerformed (ActionEvent e)
            {

                xCord -= 1;
                xLoc += 40;
                label = "LevelOne";
                ok = true;
                f.setVisible (false);
            }
        }
        );
        right.addActionListener (new ActionListener ()
        {
            public void actionPerformed (ActionEvent e)
            {

                xCord += 1;
                xLoc -= 40;
                ok = true;
                label = "LevelOne";
                f.setVisible (false);
            }
        }
        );
        down.addActionListener (new ActionListener ()
        {
            public void actionPerformed (ActionEvent e)
            {

                yCord += 1;
                yLoc -= 40;
                label = "LevelOne";
                ok = true;
                f.setVisible (false);
            }
        }
        );

        while (!ok)
        {
            try
            {
                wait ();

            }
            catch (InterruptedException e)
            {
            }
        }
        ok = false;
    }
Message was edited by:
jacob2932
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 30 2007
Added on May 31 2007
18 comments
339 views