1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

勉強メモ:Javaで親ウィンドウを閉じる方法

Posted at

導入

Java にて、JFrameをextendsしたクラスAに、JPanelをextendsしたクラスBをadd。 この時、クラスB内の処理で、クラスAのJFrameをdisposeしたいが、方法がなかなかわからなかったので記録。

問題点

クラスB内のJButtonにActionListenerを加えて、ボタンを押すと呼び出し元のJFrameを閉じたい! getparentかと思ったが、こちが取得するのは、親コンテナ(Container)なので、disposeはできない。

解決

色々調べたところ、SwingUtilitiesクラスのgetWindowAncestor()メソッドで、親ウィンドウが取得できるとのこと。 これをそのままdispose()すると、行いたい動きが実装できました。
SwingUtilities.getWindowAncestor(this).dispose();

参考
https://ateraimemo.com/Swing/WindowAncestor.html

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?