Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have problem currently for my swing reminder application, which able to minimize to tray on close. My problem here is, I need JOptionPane dialog to pop up on time according to what I set, but problem here is, when I minimize it, the dialog will pop up, but not in the top of windows when other application like explorer, firefox is running, anyone know how to pop up the dialog box on top of windows no matter what application is running?

like image 326
d1ck50n Avatar asked Jan 13 '09 09:01

d1ck50n


People also ask

How do I disable the close button in JOptionPane?

Check the code below: JOptionPane pane = new JOptionPane("message"); JDialog dialog = pane. createDialog(null, "Title"); dialog. setDefaultCloseOperation(WindowConstants.


1 Answers

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();
}
like image 61
Walter Avatar answered Sep 22 '22 02:09

Walter