1

is it possible to make Java Swing activate the parent JFrame, when closing a document modal JDialog ? The default mechanisms seems to activate the last JFrame instead of the parent JFrame. This may confuse users.

Here is some sample code to reproduce the problem:

1: Press the button "step 1: press this"
2: Press the button "step 2: press this"
3: Bring the JFrame to front with the title "step 3: activate this"
4: Close the JDialog with the title "step 4: close this"

=> now, the JFrame with the text "frame 1" should go to front.
=> instead, the JFrame with the text "step 3: activate this" goes to front.
=> for users, this is confusing, because the closed JDialog is document modal to frame 1

    public static void main(String[] args) {
    final JFrame f1 = new JFrame("frame 1");
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f1.setLayout(new FlowLayout());
    f1.add(new JButton(new AbstractAction("step 1: press this") {

        @Override
        public void actionPerformed(ActionEvent e) {
            JFrame f2 = new JFrame("step 3: activate this");
            f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f2.setLayout(new FlowLayout());
            f2.add(new JLabel("step 3: activate this"));
            f2.pack();
            f2.setVisible(true);
        }
    }));
    f1.add(new JButton(new AbstractAction("step 2: press this") {

        @Override
        public void actionPerformed(ActionEvent e) {
            JDialog d = new JDialog(f1, "step 4: close this", ModalityType.DOCUMENT_MODAL);
            d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            d.setLayout(new FlowLayout());
            d.add(new JLabel("step 4: close this"));
            d.pack();
            d.setVisible(true);
        }
    }));
    f1.pack();
    f1.setVisible(true);
}

Any ideas ? (I dont want do call f1.toFront() manually, because the results in a short "flickering" of the windows)

Best regards Marcel

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I get f1 on top and activated, not f2. – Guillaume Polet Oct 02 '12 at 12:47
  • Hi Guillaume, I assume, you have forgotten to execute step 3 (manually bring f2 to front). Regards Marcel – user1714272 Oct 02 '12 at 12:50
  • nope, tried it twice and executed all steps in the given order. f1 always ends up being on top and activated at the end of the procedure. – Guillaume Polet Oct 02 '12 at 12:55
  • 1
    Anyway, you might take a look at [this question](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) which discusses the usage of multiple frames. – Guillaume Polet Oct 02 '12 at 12:57
  • Now I tried with different jdk versions (1.6, 1.7). The problem is still absolutely reproducable in my windows-environment. And I got the error message from a customer, so it is not only bound to my machine. – user1714272 Oct 02 '12 at 13:42
  • *"This may confuse users."* Why would the last frame be any *other* than the parent frame? Now I'm confused.. – Andrew Thompson Oct 02 '12 at 23:08

0 Answers0