无模式JDialog需要在父级之上可见

Ada*_*ski 7 java swing jdialog

我的应用程序提供了启动长时间运行任务的能力.当发生这种情况时,会产生无模式JDialog,显示任务的进度.我特意使对话框无模式,以允许用户在任务运行时与GUI的其余部分进行交互.

我面临的问题是,如果对话框隐藏在桌面上的其他窗口后面,则很难找到:任务栏上没有相应的项目(在Windows 7上),Alt +下也没有可见的图标标签菜单.

有没有一种解决这个问题的惯用方法?我考虑过WindowListener在应用程序中添加一个JFrame并使用它来将JDialog放到前台.然而,这可能会变得令人沮丧(因为可能这意味着JFrame会失去焦点).

Gui*_*let 8

您可以创建非模态对话框并为其指定父框架/对话框.当您调出父框架/对话框时,它还会带来非模态对话框.

这样的事情说明了这一点:

public static void main(String[] args) throws IOException {
    JFrame frame = new JFrame();
    frame.setTitle("frame");
    JDialog dialog = new JDialog(frame, false);
    dialog.setTitle("dialog");
    final JButton button = new JButton("Click me");
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(button, "Hello");
        }
    });
    final JButton button2 = new JButton("Click me too");
    button2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(button2, "Hello dialog");
        }
    });
    frame.add(button);
    dialog.add(button2);
    frame.pack();
    dialog.pack();
    frame.setVisible(true);
    dialog.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)