I\'m working on a Java school assignment, thought I had it all finished, but then went over the requirements one more time and realized I overlooked a specific requirement and n
looks like you got everything right!
Now what I assume your teacher means is: the handler should be an instance (not "object") of an inner class that extends the WindowAdapter
class.
By doing the following:
new WindowAdapter() {
// Use annotations, it's useful :)
@Override
public void windowCloses(WindowEvent e) {
// ...
}
}
... you're actually creating a new anonymous class that extends WindowAdapter
. In this new definition, you override the WindowAdapter#windowCloses
method, then you instantiate it in a new object that you pass as an argument to addWindowListener
.
Apparently, your teacher expects you to actually define an inner class and create a new instance of it. I don't wanna just give you the code for this but you're god damn close, you'll figure it out in an instant!
To what I can understand, this should help :
addWindowListener(new WindowAdapter() {
public void windowCloses(WindowEvent e) {
try{
int confirmed = JOptionPane.showConfirmDialog(null,
"This is my handler. Shall I Exit?", "Exit Message Box, JOptionPane.YES_NO_OPTION);
if (confirmed == JOptionPane.YES_OPTION) {
dispose();
}
}
catch(Exception ex){
//Error print
e.getWindow.dispose()
}
});