Reputation: 25
I'm working in the netbeans GUI interface, and I want to know how to close the window. I found the following code:
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
However, I cannot find the name of the frame in the code.
How do I find the frame name, or am I going about this the wrong way? How do I close it?
Upvotes: 0
Views: 1915
Reputation: 25
Found an answer at https://netbeans.org/kb/articles/gui-functionality.html
Turns out all I needed was
System.exit(0);
Upvotes: 0
Reputation: 324118
How do I close it?
When you create the frame you need code like:
Jframe frame = new JFrame(...);
frame.setDefaultCloseOperation(JFrame.EXIT__ON_CLOSE);
Now when you click on the "close" button the application will exit.
However you may also want to close the frame by using a menu item. In this case what you want to do is create an Action
that you can add to your "Exit" JMenuItem.
Check out the Exit Action
found in Closing an Application. The Exit Action
shows how you can access the current frame in order to dispatch the windowClosing() event to the frame. So the "Exit" menu item will then function just like the user clicking on the "close" button.
Upvotes: 1
Reputation: 9
You are using NetBeans IDE, go to JFrame Properties, the very first option is DefaultCloseOperation, use the drop down menu to toggle between options available or you can add custom codes.
Upvotes: 0
Reputation: 411
Put this line in your form no need to use window event
setDefaultCloseOperation(EXIT_ON_CLOSE);
Upvotes: 1