Home >Java >javaTutorial >How Can I Programmatically Simulate a JFrame's Close Button Click?

How Can I Programmatically Simulate a JFrame's Close Button Click?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-18 20:20:14293browse

How Can I Programmatically Simulate a JFrame's Close Button Click?

How to Programmatically Trigger JFrame's Close Button Behavior

In contrast to the default close operation set by setDefaultCloseOperation, you seek to replicate the behavior triggered by clicking the X close button or pressing Alt F4 (on Windows).

Dispatching a Window Closing Event

To emulate the X close button's action, the solution lies in dispatching a window closing event to the JFrame. The ExitAction from the Closing An Application class offers a convenient way to achieve this.

Usage

To invoke the desired behavior, utilize the following code snippet:

frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));

The above is the detailed content of How Can I Programmatically Simulate a JFrame's Close Button Click?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn