mac osx 上 java7 模式对话框的焦点问题 [英] Focus issues with java7 modal dialogs on mac osx

查看:36
本文介绍了mac osx 上 java7 模式对话框的焦点问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在验证一个在 mac osx 的小程序上运行的 swing 应用程序.

I've been validating a swing application that runs on an applet for mac osx.

在此验证过程中,我发现模式对话框存在以下问题:

During this validation I found the following issues with the modal dialogs:

  1. 当对话框打开并且设置为 setModal(true) 时,它会阻止根窗口的内容,但是如果您单击根窗口上的某个位置,对话框会在它下方,但它应该保留在根窗口的顶部.
  2. 如果对话框有 JTextInputField,即使单击它也不会获得焦点.

所以我创建了一个小程序来显示问题.你能帮我理解这里有什么问题吗?

So I created a small program to show the problem. Can you please help me to understand what is wrong here?

package com.macosx.tests;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class DialogExample extends JApplet{

    private static final long serialVersionUID = 1L;
    private JPanel panel;
    private JButton openDialogBtn;

    private void doStart() {
        panel = new JPanel();
        panel.setPreferredSize(new Dimension(500,500));

        openDialogBtn = new JButton("open dialog");
        openDialogBtn.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                ModalDialog dialog = new ModalDialog(panel, true);
                dialog.setVisible(true);
            }

        });
        panel.add(openDialogBtn);
        setContentPane(panel);
    }


    class ModalDialog extends JDialog {
        private static final long serialVersionUID = 1L;

        public ModalDialog(Component parent, boolean modal) {
            Dimension dimensionParentFrame = parent.getSize();
            setSize(new Dimension((parent == null) ? 300 : dimensionParentFrame.width / 2, 75));

            setModal(modal);
            setModalityType(ModalityType.APPLICATION_MODAL);

            JTextField txtField = new JTextField();
            add(txtField, BorderLayout.CENTER);
        }
    }


    @Override
    public void start() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    doStart();
                }
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

使用上述创建一个 .jar 文件(test.jar).完成后,创建一个包含以下内容的 html 文件:

Use the above to create a .jar file (test.jar). Once that is done, create a html file with the following content:

<html>
<head>
<title>Dialog test Applet</title>
</head>
<body>
<applet id="DialogTestApplet" height="800" width="600"
  code="com.macosx.tests.DialogExample"
  archive="test.jar">
</applet>
</div>
</body>
</html>

完成后,运行 html 文件.您将看到一个带有灰色背景和一个按钮的小程序.然后尝试:

When this is done, run the html file. You'll see an applet with a gray background and with a button. Then try to:

  1. 点击按钮打开对话框.之后,单击灰色区域的某处:对话框位于浏览器窗口下方,但它应该保留在顶部,对吧?
  2. 点击按钮打开对话框.之后单击对话框的文本字段并尝试编写一些内容:文本对话框没有获得焦点.

那么,我在这里做错了什么?请问有mac电脑的人可以测试一下吗?

So, what am I doing wrong here? Can someone with a mac computer test this please?

谢谢

规格:

java.vendor    Oracle Corporation
java.version   1.7.0_07
os.name        Mac OS X
os.version     10.7.4

browser        firefox 15

注意:请注意,这仅在小程序在浏览器上运行且仅在 mac osx 上运行时才会发生.

推荐答案

我找到了另一种解决方法.当窗口打开时,显示一个选项窗格几毫秒然后关闭它.它将焦点放在选项窗格上,然后返回对话框,允许忽略该错误.

I found another workaround. When the window is opened, show an optionpane for a few milliseconds and close it. It give the focus to the optionpane and then back to the dialog, allowing to ignore the bug.

将这段代码添加到您的对话框构造函数中,它应该可以工作:

Add this snipet of code to your dialog constructor and it should work:

addWindowListener(new WindowAdapter(){
public void windowOpened(WindowEvent e){
    JOptionPane pane = new JOptionPane();
    final JDialog dialog = pane.createDialog("Please Wait");
    Timer timer = new Timer(50, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
        }
    });
timer.setRepeats(false);
timer.start();
dialog.setVisible(true);
}

这篇关于mac osx 上 java7 模式对话框的焦点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆