Wednesday 15 May 2013

java - How to find the currently displaying dialog? -



java - How to find the currently displaying dialog? -

see illustration code below. after clicking illustration button few times, how find lastly dialog opened ? real use-case. user has opened 1 or 2 modal dialogs , has clicked button starting long running background task in lastly dialog , has switched application during background task running. on completion, task should display notification window right dialog parent when switching application, notification window shown on top of right 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 illustration { 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 ); homecoming dialog; } public static void main( final string[] arguments ) { swingutilities.invokelater( new runnable() { @override public void run() { final illustration example = new example(); final jdialog dialog = example.createdialog( null, 1 ); dialog.setdefaultcloseoperation( windowconstants.dispose_on_close ); dialog.pack(); dialog.setlocationrelativeto( null ); dialog.setvisible( true ); } } ); } }

you utilize keyboardfocusmanager.getcurrentkeyboardfocusmanager().getfocusedwindow() will, obviously, homecoming window current contains component current keyboard focus...

java swing

No comments:

Post a Comment