1

See the example code below. After clicking the example button a few times, how to find the last dialog opened ? This is a real use-case. A user has opened one or two modal dialogs and has clicked a button starting a long running background task in the last dialog and has then switched to another application during that background task is running. On completion, the task should display a notification window with the correct dialog as the parent so that when switching back to the application, that notification window is shown on top of the correct dialog.

import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class Example
{

    private JDialog createDialog( final Window parent, final int count )
    {
        final JDialog dialog = new JDialog( parent, "Dialog " + count );
        dialog.setModal( true );
        dialog.getContentPane().setLayout( new FlowLayout() );

        final JButton button = new JButton( "Open Dialog " + ( count + 1 ) );
        button.addActionListener( new ActionListener()
        {

            @Override
            public void actionPerformed( final ActionEvent e )
            {
                final JDialog nextDialog = createDialog( dialog, count + 1 );
                nextDialog.pack();
                nextDialog.setLocationRelativeTo( dialog );
                nextDialog.setVisible( true );
            }

        } );

        dialog.getContentPane().add( button );
        return dialog;
    }

    public static void main( final String[] arguments )
    {
        SwingUtilities.invokeLater( new Runnable()
        {

            @Override
            public void run()
            {
                final Example example = new Example();
                final JDialog dialog = example.createDialog( null, 1 );
                dialog.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
                dialog.pack();
                dialog.setLocationRelativeTo( null );
                dialog.setVisible( true );
            }

        } );
    }

}

1 Answers1

2

You could use KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow() which will, obviously, return the window which current contains the component with the current keyboard focus...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Not quite what I am looking for. A window may well be showing without having the focus. The question is not about finding the window of the current focus owner. It's really about finding the upfront modal dialog of some frame - no matter if that frame has focus or not. A long running background task shall bring up a notification window after completion. While running, the user may have switched to a different application without minimizing the frame etc. – Christian Schulte Oct 29 '14 at 06:06
  • The get the child windows of the focused window...the question then is which window would you choose? – MadProgrammer Oct 29 '14 at 06:12
  • As I said, you can find the window that last had focus (triggered the button), but beyond that, there's not much else you can detect. You might be able to use something like [this](http://stackoverflow.com/questions/1354254/get-current-active-windows-title-in-java), but it is probably equating to the same thing. – MadProgrammer Oct 29 '14 at 07:01
  • Are you really sure there is no way to get a reference to the toplevel/upfront modal dialog of a frame currently showing ? This has nothing to do with focus! I looked at 'getOwnedWindows' but unfortunately you cannot remove a child from that list. So once a dialog got created it will be contained in that 'ownedWindowList' even when diposed later which makes this mechanism unusable. – Christian Schulte Oct 29 '14 at 09:08
  • The top level, modal dialog HAS to be the window with current focus, it's not possible for any child windows to have focus while a modal dialog is visible... – MadProgrammer Oct 29 '14 at 09:11
  • The application is running in the background and does not have any focus at the time the dialog needs to be found. There are some dialogs showing on the desktop but non of them are actually enabled/activated. So there is neither an active window nor a focused window. There must be an easy solution to this via 'java.awt.Window.getWindows()' I am overlooking. – Christian Schulte Oct 29 '14 at 09:59
  • Ok, I've hacked my brain for ideas, but can't think of anything that would allow you to determine the "front" window where there is no focus... – MadProgrammer Oct 29 '14 at 10:46