java - JOptionPane.showMessageDialog(null, ...) always showing dialog on primary screen -


is there solution that? alternative library might use display dialog on mouse pointer's screen (can't use parent that)? or there api change dialog's screen?

i tried using non-visible parent jframe on desired screen, has effect on dialog's screen positioning if visible when invoking dialog. "special case" have no app window or jframe around want stick dialog. should appear in center of screen user using.

instead of using joptionpane recommend instead use jdialog.

here example:

joptionpane joptionpane = new joptionpane("really this?", joptionpane.plain_message, joptionpane.yes_no_option); jdialog jdialog = joptionpane.createdialog("dialog title"); 

to display on particular screen can bounds of screen want , place dialog in center of it, example:

rectangle screenbounds = mouseinfo.getpointerinfo().getdevice().getdefaultconfiguration().getbounds();  int x = (int) screenbounds.getcenterx() - (jdialog.getwidth() / 2); int y = (int) screenbounds.getcentery() - (jdialog.getheight() / 2);  jdialog.setlocation(x, y); jdialog.setvisible(true); 

check results:

object selectedvalue = joptionpane.getvalue(); int dialogresult = joptionpane.closed_option; if (selectedvalue != null) {     dialogresult = integer.parseint(selectedvalue.tostring()); }  switch (dialogresult) {     case joptionpane.yes_option:         log.info("yes pressed");         break;     case joptionpane.no_option:         log.info("no pressed");         break;     case joptionpane.closed_option:         log.info("closed");         break;     default: } 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -