问题
I'd like to display a modal (progress) dialog above a Frame, even if that Frame isn't the active window. However, displaying a dialog causes it or its owner to become the active window. Is there a way to display a dialog without it or its owner becoming the active window -- specifically, on a Windows platform?
The reason I'd like to do this is: periodically my Frame refreshes itself and displays its modal progress dialog while refreshing. Right now my application only refreshes while it's the active window.
SSCCE, since people are asking for it:
Frame frame = new JFrame();
frame.setVisible( true );
// 30 seconds later (using a Timer/TimerTask)
Dialog dialog = new JDialog( frame, ModalityType.DOCUMENT_MODAL );
dialog.setVisible( true );
回答1:
I don't think you can do this with a modal dialog.
However the following works for a non-modal dialog:
dialog.setFocusableWindowState( false );
dialog.setVisible(true);
dialog.setFocusableWindowState( true );
When the dialog displays it will not have focus, but if you click on it, it will gain focus. You could try this on a modal dialog to see what happens.
来源:https://stackoverflow.com/questions/17179548/can-java-display-a-dialog-without-changing-the-active-window