开发者

Swing - How to close a form from inside? How to close the form without affecting the parent form?

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜