I am creating a form in Swing toolkit.
When I want to close it, I go to the JFrame and set it to visible false. Since the frame creates a开发者_开发知识库 Java class of the form, I can easily do it from outside.When I have a cancel button I usually send a listener inside the form that calls the outer:
Jframe.setVisible (false)
Is there a better way ?
The SwingUtilities
class provide a method to get the window ancestor of a given component.
You can gat the parent Window and call setVisible
or dispose
or perhaps only an event.
Window window = SwingUtilities.getWindowAncestor(this);
window.setVisible( false );
// OR
window.dispose();
// OR
WindowListener[] windowListeners = window.getWindowListeners();
windowListeners[0].windowClosing( null );
try these if you do not need to code much! click here to view image
精彩评论