开发者

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

开发者 https://www.devze.com 2023-03-06 08:11 出处:网络
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.

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

精彩评论

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