JOptionPane won't show its dialog on top of other windows

社会主义新天地 提交于 2019-11-28 11:01:28
Walter

Create an empty respectively dummy JFrame, set it always on top and use it as the component for the JOptionPane instead of null. So the JOptionPane remains always on top over all other windows of an application. You can also determine where the JOptionPane appears on screen with the location of the dummy JFrame.

JFrame frmOpt;  //dummy JFrame

private void question() {
    if (frmOpt == null) {
        frmOpt = new JFrame();
    }
    frmOpt.setVisible(true);
    frmOpt.setLocation(100, 100);
    frmOpt.setAlwaysOnTop(true);
    String[] options = {"delete", "hide", "break"};
    int response = JOptionPane.showOptionDialog(frmOpt, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, "delete");
    if (response == JOptionPane.YES_OPTION) {
        removeRow();
    }
    frmOpt.dispose();
}

Old post, but I was struggling with this.

My problem was more with Javafx allowing the JOptionPane to go behind the current Java window.

Therefore I used the following which does what the original poster asked by putting the JOptionPane in front of all windows; even JAVAFX.

Firstly the old JOptionPane:

JOptionPane.showMessageDialog(null, "Here I am");

Now an JOptionPane that stays in front:

final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);    
JOptionPane.showMessageDialog(dialog, "Here I am");

And for fun here is everything in one long line:

JOptionPane.showMessageDialog(
                 ((Supplier<JDialog>) () -> {final JDialog dialog = new JDialog(); dialog.setAlwaysOnTop(true); return dialog;}).get()
                 , "Here I am");

You can make a static method some where that will return the JDialog for you and then just call it in the JOptionPane to clean up your code a bit.

Are you using one of the canned JOptionPanes? (Like JOptionPane.showCOnfirmDialog(...))

You may want to look at extending JDialog and making your own dialog panel, and then calling myDialog.setAlwaysOnTop(true);

Windows is blocking this operation since XP.

The scenario before was like: Your a tiping in some text in an editor and not recognize that another dialog is coming to front when you are tipping the text. The coming dialog gets the focus and you are tiping in the new dialog. Maybe you click enter after you are ready and do this in the wrong dialog, which is asking whether you realy want to delet your hard disk ;)

The come to front call in java is only working for java windows.

The possibibilty to notify the user of a new window is to implement a Frame, which will highlighted/flashing in the windows task bar.

d1ck50n

Correction the post above..

I have resolve my problem as below:

this.setVisible(true); // show main frame
MyDialog dialog = New MyDialog(this, true); // show my custom dialog
dialog.setVisible(true);
this.setVisible(false);

it works fine for me :)

ShawnD

You might think about using a JFrame instead. It may give you a little more flexibility.

If you are using a JFrame and you want it to popup on top of the other windows use:

myFrame.setVisible(true);
myFrame.setState(Frame.NORMAL);

The setState will show the window to the user if it was in minimized state previously.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!