-1

I am a beginner and not so familar with Java yet, so the solution to this might be simple.

I have one MainClass that includes the main method. This MainClass creates a JFrame which can have various JPanels inside. I have one class called CommandInput which creates a JPanel containing a JTextArea. Now I want that when the user closes the JFrame of the MainClass that it asks if he wants to save the changes. As the JPanels inside the JFrame vary I do not really want to include this in the MainClass. I know that it is possible for every "SubClass" like the CommandInput to add a windowListener to the JFrame of the MainClass but that does not really seems efficient to me:

public class CommandInput {
    public CommandInput(JFrame mainFrame) {
        mainFrame.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override //Overwrites the normal behavior of this method
            public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                System.out.println("Closing Event triggered and detected by CommandInput object");
            }
        });
    }
}

Should I maybe rather have one WindowAdapter in the MainClass and add this code to its windowClosing method? And if so how would I do that?

Marcono1234
  • 5,856
  • 1
  • 25
  • 43
  • 1
    possible duplicate of [How to save application options before exit?](http://stackoverflow.com/questions/2361510/how-to-save-application-options-before-exit) – Storm- Jul 26 '15 at 13:09
  • Accidentally made a comment, and can't figure out how to delete comments :P – Storm- Jul 26 '15 at 13:09
  • Thank you for the link @Storm-, but that does not quite solve my problem. I have SubClasses that should add their own code to the `windowClosing` event of the JFrame from the MainClass without having them all add an extra `WindowListener`, is this possible? – Marcono1234 Jul 26 '15 at 13:23

2 Answers2

1

I now solved it in a way that each SubClass adds an anonymous inner class of an interface containing a method that returns if the JFrame can be closed to a list. Only if all of the anonymous inner classes in the list return true the JFrame closes:

public interface ClosingListener {
    public boolean allowClosing();
}

public MainClass {
    private ArrayList<ClosingListener> closingListeners = new ArrayList<ClosingListener>();

    public MainClass() {
        JFrame frame = new JFrame();
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                boolean canClose = true;
                for (ClosingListener closingListener : closingListeners) {
                    if (!closingListener.allowClosing()) {
                        canClose = false;
                        break;
                    }
                }

                if (canClose) {
                    System.exit(0);
                }
            }
        });
    }

    public void addClosingListener(ClosingListener closingListener) {
        closingListeners.add(closingListener);
    }
}
Marcono1234
  • 5,856
  • 1
  • 25
  • 43
0

May be you need this

addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                int choice = JOptionPane.showConfirmDialog(null, "Save changes?");
                if(choice==0) System.exit(0);
                }
});
Rahul
  • 289
  • 2
  • 7
  • No sorry I was rather asking for a way to modify his `windowClosing` method instead of adding for every component of the JFrame a new WindowListener to the JFrame. – Marcono1234 Jul 26 '15 at 18:21