0

I am creating a simple text editor and have run into a small problem. I can open and have open several new windows for creating text documents by clicking on the new button in the menu. My problem is if I click on "File" then "Exit" from the menu it closes all the windows not just the one I wanted closed. How do I go about making it so that it will close just the chosen window and not the entire application.

Here's some code:

public void actionPerformed(ActionEvent event) {
    if(event.getSource() == newFile) {
        new EditorGUI();
    } else if(event.getSource() == openFile) {
        JFileChooser open = new JFileChooser();
        open.showOpenDialog(null);
        File file = open.getSelectedFile();             
        openingFiles(file);
    }  else if(event.getSource() == exit) {
        System.exit(0);
    }
}

It works however when I click on the X at the top right hand corner of the window:

private JFrame createEditorWindow() {
    editorWindow = new JFrame("JavaEdit");
    editorWindow.setVisible(true);
    editorWindow.setExtendedState(Frame.MAXIMIZED_BOTH);
    editorWindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    // Create Menu Bar
    editorWindow.setJMenuBar(createMenuBar());
    editorWindow.add(scroll, BorderLayout.CENTER);
    editorWindow.pack();
    // Centers application on screen
    editorWindow.setLocationRelativeTo(null);

    return editorWindow;
}

Screenshot: enter image description here

Schonge
  • 332
  • 2
  • 6
  • 17
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) for some background on why multiple frames are *usually* inadvisable. OTOH this sounds like one of the few 'corner cases' where having multiple instances of frame (one for each document being edited) might make sense for the user. In that event, scroll down to some other answers that discuss that use-case. – Andrew Thompson May 21 '14 at 00:34

3 Answers3

4

Don't use multiple frames. An application should only have a single main JFrame.

Then the child window should be a JDialog. A JDialog will only support DISPOSE_ON_CLOSE so you don't have to worry about exiting the application.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Will a JDialog provide me with the same functionality? – Schonge May 20 '14 at 21:09
  • Yes, it has a content pane, just like a JFrame. – camickr May 20 '14 at 21:10
  • 1
    I have to say that I Think this is one of those 'corner cases' where multiple frames actually makes sense. AFAIU there are 3 basic approaches for a Multi Document editor. 1) One frame with a tabbed pane. Tab for each document. 2) One frame with a desktop-pane/internal frames. Internal frame for each document. 3) Separate frames. One per document. -- (1) might be a suitable alternative, though as a user I'd probably prefer a choice between that and separate frames. – Andrew Thompson May 21 '14 at 00:38
  • Agreed you could use multiple frames, that causes multiples frames to be displayed on the task bar and can cause window clutter. However, if the OP does use this approach then all frames should use DISPOSE_ON_CLOSE. – camickr May 21 '14 at 00:48
1

This also seems to do the trick:

if(event.getSource() == exit) {
    editorWindow.dispose();
}
Schonge
  • 332
  • 2
  • 6
  • 17
  • -1, Yes, it will do the trick but it is better design your application properly. Learn by other applications you use such as the Browser you are viewing this forum in. The browser has a main window, then when you click on certain menu items a JDialog is displayed as required. – camickr May 21 '14 at 00:04
  • That's a fair point. I submitted this project to the Code Review site to get advice on better and cleaner code structure so it would be very hypocritical of me not to follow your advice :-P – Schonge May 21 '14 at 00:38
  • Check out Andrew's comment in my answer. There is the potential to use multiple frames (I still don't think it is a good option, again think of existing editors that you might currently use). – camickr May 21 '14 at 00:51
  • Yes the tabbed pane seems like a nice feature to have in the editor so I may revert to something like that! – Schonge May 21 '14 at 00:54
1

You could code something like this:

private JFrame createEditorWindow() {
    JFrame editorWindow = new JFrame("JavaEdit");
    editorWindow.setExtendedState(JFrame.MAXIMIZED_BOTH);
    editorWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    editorWindow.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent event) {
            exitProcedure();
        }
    });
    // Create Menu Bar
    editorWindow.setJMenuBar(createMenuBar());
    editorWindow.add(scroll, BorderLayout.CENTER);
    editorWindow.pack();
    // Centers application on screen
    editorWindow.setLocationRelativeTo(null);

    editorWindow.setVisible(true);
    return editorWindow;
}

public void exitProcedure() {
    editorWindow.dispose();
}

You perform the exitProcedure in your JMenuUtem action listener for Exit.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111