Handling event problem
Hi boys and girls.
I've been working in a multiframe GUI. I have a main window (a custom class extending JFrame) with a menu.
My menu items events are beeing handled by my custom class that implements ActionListener. This cutom class, through actionPerformed(ActionEvent e) method, handles all my button events and the menu items events. To manage focus i also implemented in this class the WindowListener interface, listening to all other frames of my application, so that they can behave more or less like amodal JDialog does.
One of my menu items is a 'Quit' option. It simply exits the application if there is no changed data on a JList or asks the user if he wants to save changes first. As i said before this action is handled by actionPerformed(ActionEvent e) in my custom class.
Once the WindowListener interface is already implemented i thought i could make the frame respond the same way when the user clicks the 'X' icon just by implementing on the windowClosing(WindowEvent we) method a redirection to actionPerformed li ke this:
...
publicvoid windowClosing(WindowEvent e){
this.dispacthEvent(new ActionEvent(quitItem, ActionEvent.ACTION_PERFORMED, quitItem.getActionCommand()));
}
...
being quitItem my JMenuItem. My interpretation is that, before exiting the system, it would first launch the code that i've established within actionPerformed for the quit option.
Am i completely wrong or am i doing at least something correctly?
Please help me. Thank you
[1717 byte] By [
JCruza] at [2007-10-2 15:07:12]

I forgot to say:I'm using this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JCruza at 2007-7-13 13:59:22 >

Hi,
As i have understood the rules of window management you set a default actionlistener when you set the defaultCloseOperation. There has to be a way to override this actionlistener too do what you want.
If anyone has an answer or some thought on this problem please post it. Would be nice with a discussion on this matter ;-)
Tesa at 2007-7-13 13:59:22 >

Instead of that do this:
mainWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //This is handled by windowClosing.
mainWindow.addWindowListener(new QuitListener());
menuItem = new JMenuItem("Quit", KeyEvent.VK_Q);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription("Exit the program");
menuItem.addActionListener(new QuitListener());
menu.add(menuItem);
class QuitListener implements ActionListener, WindowListener{
public void actionPerformed(ActionEvent event) {
checkForSaveAndClose(); //Or System.exit(0) if you don't want to do something on close.
}
public void windowClosing(WindowEvent e) {
checkForSaveAndClose();
}
public void windowClosed(WindowEvent e) { }
public void windowOpened(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public void windowGainedFocus(WindowEvent e) { }
public void windowLostFocus(WindowEvent e) { }
public void windowStateChanged(WindowEvent e) { }
}
Or, at least, that's what I'm doing. As I understand it this is the more correct way. Feel free to correct me if I'm wrong.
That looks like a nice and clean way to solve the problem.
Tesa at 2007-7-13 13:59:22 >

Hi again.
First of all thank you for your replies.
I've been away for some time and couldn't manage to answer back.
However i did manage to work around the problem. This is how it goes:
Once my custom class, wich extends JFrame and is my application's main window, is the handler for all other windows, i made my program ccall the method that verifies if you should save before exiting the application in the windowClosing(WindowEvent e)
method. This works well if user clicks 'X' window icon. If the user goes to the menu and clicks 'Quit' menu item, the code redirects to the actionPerformed(ActionEvent e)
method of ActionListener interface and it does this:
public void actionPerformed(ActionEvent e){
...
if(e.getActionCommand().equals("Quit")){//If you click 'Quit' item on menu
this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING));//Dispatching a window closing event, once this class has a window listener,
//it automatically redirects code to the handler (this class)
//and to it's windowClosing method wich is already working perfeclty
}
...
}
Also i've defined the following for it:
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
At this point the following question may rise:
How does the handler class (this class), wich handles all windows, know this is the main window and that, once a window closing event is dispatching, it should leave the application?
I only had to verify if this was the correct window by doing this:
public void windowClosing(WindowEvent w){
if (w.getWindow().equals(this)) {//if this is the main window
saveOnExit();//then call the save and exit code
}
...//if not proceed as desired
}
It's working exactly as i wanted.
Once again thank you all for your thoughts and opinions.
JCruza at 2007-7-13 13:59:22 >

