views:

91

answers:

1

Following situation:

I have a JFrame and call JOptionPane.showInputDialog("test"). The modal dialog will be shown.

When I then switch to another open window in Windows (let's say Firefox) and then return to my Java application (by clicking the tab in the windows task bar or with ALT+TAB) then only the dialog will be shown.

Is it possible to show the main frame behind the dialog when switching to my app? E.g. Eclipse behaves the desired way when you open the Preferences dialog. Eclipse is SWT, but maybe it's possible in Swing too!

+2  A: 

I don't know of a way to do this with JOptionPane's static methods, since you don't have access to the dialog itself. However, you can probably achieve the same thing by making your own dialog and adding a listener like this:

    final JDialog dialog = new JDialog();
    dialog.setTitle("Test Input");
    dialog.setModal(true);


    dialog.addWindowFocusListener(new WindowFocusListener() {

        @Override
        public void windowLostFocus(WindowEvent arg0) {

        }

        @Override
        public void windowGainedFocus(WindowEvent arg0) {
            frame.toFront();        
        }
    });
Amber Shah
exactly what I was looking for! thanks
räph
You probably should use `JOptionPane`'s dialog to ensure standard platform look-and-feel for the message. It's true that you can't get the dialog with `JOptionPane`'s static methods, but you can instantiate a `JOptionPane` then call its `createDialog`.
Geoffrey Zheng