Reputation: 177
I have to save the state of the program in a xml when the user clicks the Close Button or another JButton which says Exit on it. I have it so it runs using a WindowListener when the Window is closing it runs it. But that only works if you click the Red X close button, but not when you have the other button. I could run it before I do System.exit(0) in actionListener for button but some of the classes I need information from I do not have access from that place.
This is for the frame.
frame.addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent arg0){}
public void windowClosed(WindowEvent arg0){}
public void windowClosing(WindowEvent arg0)
{
myScreen.write.writeToXmlFile(myScreen.pics, myScreen.row, myScreen.col);
}
public void windowDeactivated(WindowEvent arg0){}
public void windowDeiconified(WindowEvent arg0){}
public void windowIconified(WindowEvent arg0){}
public void windowOpened(WindowEvent arg0){}
});
This is the code in button.
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
I do not have access to myScreen from the class that has button in it.
Upvotes: 1
Views: 631
Reputation: 109813
I do not have access to myScreen from the class that has button in it.
JButton#doClick(); can do that
but correct way could be too (maybe)
public void windowClosing(WindowEvent arg0)
1st. void or class
create screenshot
2.nd void or class
setVisible(false) for Top-Level Container(s)
3rd void or class
save applications states
4th void or class
System.exit()
sure you can put all 4 voids (classes) to the one, but in a.m. case is code only more clear and readable
Upvotes: 2