开发者

Process won't close after adding mouse listener

开发者 https://www.devze.com 2023-01-06 06:58 出处:网络
I want to know what the issue is when I try to close my Java Swing application when I add a listener to the form frameview. Here\'s how to produce my problem: I开发者_StackOverflow中文版n Netbeans, cr

I want to know what the issue is when I try to close my Java Swing application when I add a listener to the form frameview. Here's how to produce my problem: I开发者_StackOverflow中文版n Netbeans, create a new Swing application project, then in the main class, create a local method that adds an empty mouse listener to the main window via this.getMainFrame().addMouseListener(new MouseAdapter(){}). Go into the form class, and call that method using .getApplication().createListenerMethod() in the form's constructor, right after InitComponents().

Run the program and you'll see that after closing it, netbean's little progress bar says it's still running :(

I find that if I set the form's default close operation to "dispose", it solves the problem. Why does it do this if I call a method from the form to the main application class to add a listener to itself? The reason why I'm adding it like this is that I want to keep the main application logic in the main file class, I feel uneasy writing code a in form designer parsed class.


I don't think that the mouse listener has anything to do with what you see, though it's possible that the NB code-generation wizard does some weird stuff in the protected regions.

Swing does not auto-dispose the frames/windows in case you want to reuse them (show/hide multiple times). Usually you want to keep track of closed windows/dispose them from a window listener or in simpler cases, use JFrame.setDefaultCloseOperation(). Most often each reasonably big Swing application has some sort of global state-tracking facility that disposes all windows and shuts down thread pools when the termination condition is met.

You can use NetBeans RCP as well that takes care of many aspects of a GUI application if you are willing to invest the time to learn it (I would not recommend for small apps). Another alternatives for Swing app frameworks are are Jide Software's JDAF (my preferred) and "Better Swing Application Framework" (from kenai.com)

0

精彩评论

暂无评论...
验证码 换一张
取 消