如何显示的所有窗口顶部的JOptionPane(how to show JOptionPane on

2019-06-25 03:55发布

我创建了一个DialogUtil这说明在不同的情况下JOptionPan的数字。 有时在我的动作类调用此方法具有如下空参数。

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

在这种情况下的JOptionPane不会对窗口的顶部出现。

我怎么能添加一些JOptionPane中总是出现在顶部?

public static void showNotExist(JPanel panel, String action) {
    JOptionPane.showMessageDialog(panel, new JLabel(action.concat(" doesn't exist."), 2));
}

Answer 1:

有两个可能的问题

  • JOptionPane的叫出来EDT,那么只有工具栏(标题,从本机OS来是在屏幕上可见,是的rootPane不可见)在屏幕上可见

  • 那里你可以测试JOptionPanes功能,其中JOptionPane.showInternalMessageDialog()让烦恼在所有情况下,还有另一种的JDialog与setModal(真),真正的原因我不知道,同样应该与ModalityTypes

  • 无法表示在同一时间屏幕上的两个JOptionPanes

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");
            }
        });
    }
}


Answer 2:

您是否尝试过这样的事情?

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

谁也不能保证,该操作系统将让你的对话框始终在最前面,但它通常会工作。

如果你有一个现有的窗口或对话框,你想带它去顶,但不想永久设置alwaysOnTop,同时保留alwaysOnTop的旧值这应该工作:

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

只运行在SwingThread代码。



Answer 3:

您可以通过使用此代码之上始终设置的JOptionPane: -

        JFrame jf=new JFrame();
        jf.setAlwaysOnTop(true);
        int response = JOptionPane.showConfirmDialog(jf,"Message", "Title", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);


Answer 4:

我不知道是什么WebOptionPaneWebPanel是的,但如果他们是根据JOptionPane ,那么问题是,你传递null对于第一个参数showXXX()方法。 如果您希望JOptionPane是模态-这迫使它是在指定窗口的前面-那么你需要指定窗口(即JFrame -对于第一个参数。



Answer 5:

public static void showNotExist(JPanel panel, String action) {
    JOptionPane.showMessageDialog(rootPane, new JLabel(action.concat(" doesn't exist."), 2));
}

先给的rootPane作为showMessageDialog区第1值



Answer 6:

如果你的类已经扩展JFrame的,然后只是简单的设置类属性setAlwaysOnTop(真); 在任何地方的构造函数之前JOptionPane.showMessageDialog(NULL,“还行”);

我用它来复制文件,并检查,甚至不需要一个JFrame但JOptionPane的。

PS。如果您不希望主要的JFrame始终显示在上面,那么你需要创建空的JFrame或重置的属性setAlwaysOnTop(假); 之后的JOptionPane。



文章来源: how to show JOptionPane on the top of all windows