how to show JOptionPane on the top of all windows

前端 未结 6 1020
予麋鹿
予麋鹿 2021-01-07 21:01

I have created a DialogUtil which shows numbers of JOptionPan in different situation. sometimes in my action class call to this method with null parameters as below.

6条回答
  •  猫巷女王i
    2021-01-07 21:26

    Have you tried something like this?

    JOptionPane optionPane = new JOptionPane();
    JDialog dialog = optionPane.createDialog("Title");
    dialog.setAlwaysOnTop(alwaysOnTop);
    dialog.setVisible(true);
    

    There is no guarantee that the operating system will allow your dialog to be always on top, but it will often work.

    If you have an existing window or dialog and you want to bring it to the top, but don't want to permanently set alwaysOnTop, this should work while leaving the old value of alwaysOnTop alone:

    boolean supported = window.isAlwaysOnTopSupported();
    boolean old_alwaysOnTop = window.isAlwaysOnTop();
    if (supported) {
      window.setAlwaysOnTop(true);
    }
    window.toFront();
    window.requestFocus();
    if (supported) {
      window.setAlwaysOnTop(old_alwaysOnTop);
    }
    

    Run that code only on the SwingThread.

提交回复
热议问题