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

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

问题描述

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

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

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

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

package com.macosx.tests;导入 java.awt.*;导入 java.awt.event.ActionEvent;导入 java.awt.event.ActionListener;导入 javax.swing.*;公共类 DialogExample 扩展 JApplet{private static final long serialVersionUID = 1L;私人 JPanel 面板;私人 JButton openDialogBtn;私有无效 doStart() {面板 = 新 JPanel();panel.setPreferredSize(new Dimension(500,500));openDialogBtn = new JButton("打开对话框");openDialogBtn.addActionListener(new ActionListener(){@覆盖公共无效动作执行(ActionEvent arg0){ModalDialog dialog = new ModalDialog(panel, true);dialog.setVisible(true);}});panel.add(openDialogBtn);设置内容窗格(面板);}类 ModalDialog 扩展 JDialog {private static final long serialVersionUID = 1L;公共模态对话框(组件父级,布尔模态){维度维度ParentFrame = parent.getSize();setSize(new Dimension((parent == null) ? 300 : dimensionParentFrame.width/2, 75));setModal(模态);setModalityType(ModalityType.APPLICATION_MODAL);JTextField txtField = new JTextField();添加(txtField,BorderLayout.CENTER);}}@覆盖公共无效开始(){尝试 {SwingUtilities.invokeAndWait(new Runnable() {公共无效运行(){开始();}});} 捕获(异常 e){抛出新的运行时异常(e);}}}

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

<头><title>对话测试小程序</title><身体><applet id="DialogTestApplet" height="800" width="600"代码="com.macosx.tests.DialogExample"存档=test.jar"></小程序>

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

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

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

谢谢

规格:

java.vendor Oracle Corporationjava.version 1.7.0_07os.name Mac OS X操作系统版本 10.7.4浏览器火狐 15

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

解决方案

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

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

addWindowListener(new WindowAdapter(){public void windowOpened(WindowEvent e){JOptionPane 窗格 = new JOptionPane();final JDialog dialog = pane.createDialog("请稍候");定时器 timer = new Timer(50, new ActionListener() {public void actionPerformed(ActionEvent e) {对话框处理();}});timer.setRepeats(false);定时器开始();dialog.setVisible(true);}

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. When a dialog is open and is setModal(true) it blocks the content of the root window, but if you click somewhere on the root window, the dialog goes under it, but it should remain on the top of the root window.
  2. If the dialog has a JTextInputField it does not receive focus even when you click on it.

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

}

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>

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. click on the button to open the dialog. After that, click somewhere on the gray area: the dialog goes under the browser window but it should remain on the top, right?
  2. click on the button to open the dialog. After that click on the textfield of the dialog and try to write something: the textdialog does not receive focus.

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

Thanks

Specs:

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

browser        firefox 15

NOTE: please note that this is only happening when the applet runs on the browser and only on 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天全站免登陆