Re: Weird window close behavior



mrstephengross wrote:
I have a short swing program (see below) that dislpays a HelloWorld
frame. It compiles fine, but runtime behavior is a bit awkward. To
wit:

(1) I run the program. It shows a nice 100x100 empty panel. I move the
mouse directly over the CLOSE button (the top-right "X") WITHOUT
moving over the content pane at all. I click the close button. The
window closes.

(2) I run the program. It shows a nice 100x100 empty panel. I move the
mouse first over the content pane, THEN over the CLOSE button and
click it. The window does NOT close.

This is indeed weird behavior. I wonder if it has anything to do with
my system configuration. I am running:
OS: Ubuntu 6
Javac: gcj-4.2 (GCC) 4.3.2 (Ubuntu 4.2.3-2ubuntu6)
java: "1.5.0" gij (GNU libgcj) version 4.2.3 (Ubuntu 4.2.3-2ubuntu6)

Any ideas?

=== CODE FOLLOWS ===
import javax.swing.JFrame;

public class HelloWorldFrame {

public static void main(String args[]) {
new HelloWorldFrame();
}
HelloWorldFrame() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 100);
frame.setVisible(true);
}
}
=== EOF ===
It is possible that because you didn't create the JFrame instance in the Event Dispatch Thread, you've cause some strange race condition.

Always make sure you are in the EDT. If you know you're not, or aren't sure, use EventQueue.invokeLater(...) to ensure your code is in the EDT.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
.


Quantcast