0

I've got an application in which sometimes I use "modal", smaller windows. To simulate such modal behaviour what I do is defining them as extensions of JFrames, disabling the main application frame and setting the default close operation to do nothing. The problem of this is that JFrame can't have a parent so, when the user clicks the button said to close the window, it in fact closes and the system goes back to the last used application, which is not always the desired effect (e.g. the user opens the modal window, then he/she Alt+Tabs into another application, then switches back into the modal window and closes it, resulting this in being presented this last application, instead the main frame of mine). Is there any way to do this (binding JFrame to a parent or something like that...?). Note that JOptionPane is not an option because these smaller windows need to have like twenty different and custom Components inside each.

  • 2
    Why not you go for `JDesktopPane` ?? YOu can look at here to know about how to use it: http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html – Vishal K Mar 12 '13 at 18:14
  • Looks like a good option, but JDialog seems to be easier to use since JDesktopPane is a container of a JFrame and as such I'd still require to have the JFrame, but JDialog allows me to simple replace "extends JFrame" with "extends JDialog". Anyway, upvote because I had never heard of this class and looks useful. – Jorge Antonio Díaz-Benito Mar 12 '13 at 18:27
  • `JDesktopPane` is the container of `JInternalFrame` not `JFrame`.. – Vishal K Mar 12 '13 at 18:29
  • Ah yeah sorry I was wrong, so a JDesktopPane can be used as parameter to setContentPane of a JFrame, that's the way you were suggesting, isn't it? Thanks! – Jorge Antonio Díaz-Benito Mar 12 '13 at 18:48
  • 1
    See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Mar 13 '13 at 00:41

2 Answers2

2

Why don't you use a JDialog? :-)

D.R.
  • 20,268
  • 21
  • 102
  • 205
0

You could add a JFrame parameter to the constructor and internally save it as parent, and save a reference of the child in a list in the parent. Then you can override the dispose method and close all children/parent before closing the window.

For example:

public class ParentWindow extends JFrame {

    //Here you add children when you create them
    List<JFrame> children = new ArrayList<JFrame>();

    @Override
    public void dispose() {
        for(JFrame f : children) {
            f.dispose();
        }
        super.dispose();
    }

    public void randomMethod() {
        JFrame newWindow = new JFrame(this);
        children.add(newWindow);
    }
}

public class ChildWindow extends JFrame {

    //Here you save the parent window
    JFrame parent;
    public ChildWindow(JFrame parent) {
         this.parent = parent;
    }

    @Override
    public void dispose() {
        parent.dispose();
        super.dispose();
    }
}
germanfr
  • 547
  • 5
  • 19