Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Basic Swing question

  Asked By: Kuhaylah    Date: Mar 06    Category: Java    Views: 689
  

I've created an Action class "ExitAction" which is added to a JMenuItem.

So, when user click on File->Exit, the program exits as expected.


However, if the user click on "X" on the upper corner, ExitAction is
not called.

How do I go about associating ExitAction with JFrame??

I've implemented a MyWindowListener, but I can't seem to invoke
ExitAction from there.

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Scott Simmons     Answered On: Mar 06

Since there is certainly more than one way for your program  to exit, maybe you
should separate the logic of you ExitAction class  to another method and/or
class. Then your ExitAction consists merely of a method call. The
windowlistener can then call that same method in response to a window closing
event. I am unsure how one would fire an event themselves so that ExitAction
could catch it. Perhaps someone else knows how to do this?

 
Answer #2    Answered By: Raju Srinivas     Answered On: Mar 06

Implement a windowClosed() listener and call the ExitAction

 
Answer #3    Answered By: Neil Turner     Answered On: Mar 06

You can use JFrame.addWindowListener(java.awt.event.WindowAdapter)

 
Answer #4    Answered By: Katrina Edwards     Answered On: Mar 06

Maybe try something like this in your JFrame constructor:

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
//TODO: - Call your Exit Action Code from here
}
} );

( or else use your JFrame object to make these calls.... )

 
Didn't find what you were looking for? Find more on Basic Swing question Or get search suggestion and latest updates.




Tagged: