18

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.

DialogUtil.showNotExist(null,xml.getName().concat(" is null or"));

In this case JOptionPane does not appears on the top of window.

How can I add something to JOptionPane to appears always on the top?

public static void showNotExist(JPanel panel, String action) {
    JOptionPane.showMessageDialog(panel, new JLabel(action.concat(" doesn't exist."), 2));
}
itro
  • 7,006
  • 27
  • 78
  • 121
  • Simply pass the parent as an argument, instead of `JPanel` , let it be the main container i.e. `JFrame`, then you really doesn't have to bother about such things :-) – nIcE cOw Jun 04 '12 at 13:29
  • *"sometimes in my action class call to this method with null parameters as below."* Why `null`? – Andrew Thompson Jun 05 '12 at 01:41
  • 1
    Because i call this method in the body of a class which has no reference to GUI or frame. – itro Jun 05 '12 at 11:39

6 Answers6

19

You can set JOptionPane always on top by using this code:-

JFrame jf=new JFrame();
jf.setAlwaysOnTop(true);
int response = JOptionPane.showConfirmDialog(jf,"Message", "Title", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Arvind Kumar
  • 400
  • 3
  • 11
18

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.

Enwired
  • 1,563
  • 1
  • 12
  • 26
5

there are two possible issues

  • JOptionPane is called out of EDT, then only toolbar (caption that came from Native OS is visible on the screen, RootPane isn't visible) is visible on the screen

  • there you can to test JOptionPanes features, where JOptionPane.showInternalMessageDialog() makes troubles in all cases that there is another JDialog with setModal(true), real reason I dont know, same should be with ModalityTypes

  • not possible to showing two JOptionPanes on the screen in the same time

code

import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.Timer;
//http://stackoverflow.com/questions/8670297/make-java-swing-modal-dialog-behave-like-mac-osx-dialogs
public class ModalDialogDemoFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private ModalDialogDemoFrame modalDialogDemo;

    public ModalDialogDemoFrame() {
        modalDialogDemo = this;
        setBounds(100, 100, 400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton buttonDialog = new JButton("Open Dialog");
        buttonDialog.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                // Create a Modal Dialog with this Frame as Parent.
                ModalDialog modalDialog = new ModalDialog(modalDialogDemo, true);
                modalDialog.setVisible(true);
            }
        });
        getContentPane().add(buttonDialog, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    ModalDialogDemoFrame window = new ModalDialogDemoFrame();
                    window.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
//http://stackoverflow.com/questions/4577424/distinguish-between-a-single-click-and-a-double-click-in-java/4577475#4577475
class ClickListener extends MouseAdapter implements ActionListener {

    private final static int clickInterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
    private MouseEvent lastEvent;
    private Timer timer;

    public ClickListener() {
        this(clickInterval);
    }

    public ClickListener(int delay) {
        timer = new Timer(delay, this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() > 2) {
            return;
        }
        lastEvent = e;
        if (timer.isRunning()) {
            timer.stop();
            doubleClick(lastEvent);
        } else {
            timer.restart();
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        timer.stop();
        singleClick(lastEvent);
    }

    public void singleClick(MouseEvent e) {
    }

    public void doubleClick(MouseEvent e) {
    }
}

class ModalDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    public ModalDialog(JFrame parent, boolean modal) {
        Dimension dimensionParentFrame = parent.getSize();
        setSize(new Dimension((parent == null) ? 300 : dimensionParentFrame.width / 2, 75));
        Dimension dimensionDialog = getSize();
        int x = parent.getX() + ((dimensionParentFrame.width - dimensionDialog.width) / 2);
        setLocation(x, parent.getY() + parent.getInsets().top);
        //setUndecorated(true);
        setModal(modal);
        //setUndecorated(true);
        //getRootPane().setWindowDecorationStyle(JRootPane.ERROR_DIALOG);
        setModalityType(ModalityType.APPLICATION_MODAL);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        final JButton buttonClose = new JButton("Close");
        buttonClose.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
//ok
                /*JOptionPane.showMessageDialog(buttonClose,
                "Eggs are not supposed to be green.",
                "Inane warning",
                JOptionPane.WARNING_MESSAGE);*/
//uncomment for un_handled GUI, JOptionPane is behing JFrame I think....
                /*JOptionPane.showInternalMessageDialog(buttonClose,
                "Eggs are not supposed to be green.",
                "Inane warning",
                JOptionPane.WARNING_MESSAGE);*/
//ok
                /*JOptionPane.showConfirmDialog(buttonClose,
                "Eggs are not supposed to be green.",
                "Inane warning",
                JOptionPane.WARNING_MESSAGE);*/
//ok                
                /*JOptionPane.showMessageDialog(null,
                "Eggs are not supposed to be green.",
                "Inane warning",
                JOptionPane.WARNING_MESSAGE);*/
//uncomment for un_handled GUI
//Exception occurred during event dispatching:
//java.lang.RuntimeException: JOptionPane: parentComponent does not have a valid parent                
                /*JOptionPane.showInternalMessageDialog(null,
                "Eggs are not supposed to be green.",
                "Inane warning",
                JOptionPane.WARNING_MESSAGE);*/
//ok                
                JOptionPane.showConfirmDialog(null,
                        "Eggs are not supposed to be green.",
                        "Inane warning",
                        JOptionPane.WARNING_MESSAGE);
                dispose();
            }
        });
        add(buttonClose, BorderLayout.CENTER); // comment for listening
        addMouseListener(new ClickListener() {

            @Override
            public void singleClick(MouseEvent e) {
                System.out.println("single");
            }

            @Override
            public void doubleClick(MouseEvent e) {
                System.out.println("double");
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
4

I don't know what WebOptionPane or WebPanel are, but if they're based on JOptionPane, then the issue is that you're passing null for that first argument to the showXXX() method. If you want the JOptionPane to be modal -- which forces it to be in front of a specified window -- then you need to specify a window (i.e., a JFrame -- for that first argument.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • You are right and I'm aware of it but I want to be on the top what so ever is the first argument and without specify a windows? – itro Jun 05 '12 at 11:34
  • you are very funny.thanks for your unicorn, I means i want JOPtionPan be on the top of windows. – itro Jun 06 '12 at 08:40
  • @itro, my point is that you can want something that's not realistic to get. You have to specify a frame; if it must be the frontmost frame, then you have to figure out which one that is. There's a static method `Frame.getFrames()` that lists all existing `Frame` objects, and there's a method `Window.getFocusOwner()` which you can use to figure out if a `Frame` is the frontmost one. Your utility method needs to find the right `Frame` and use it. – Ernest Friedman-Hill Jun 06 '12 at 11:14
1
public static void showNotExist(JPanel panel, String action) {
    JOptionPane.showMessageDialog(rootPane, new JLabel(action.concat(" doesn't exist."), 2));
}

Try giving the rootpane as the 1st value in the showMessageDialog section

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
BasuruK
  • 61
  • 1
  • 8
1

If your class has extended JFrame, then just simply set the class property setAlwaysOnTop(true); in anywhere in constructors before JOptionPane.showMessageDialog(null,"OKay");

I use it for copying file and check, don't even need a JFrame but JOptionPane.

P.S. If you don't want the main JFrame always shows on the top, then you need to create dummy JFrame or reset the property setAlwaysOnTop(false); after the JOptionPane.

Ken
  • 11
  • 3