简体   繁体   English

Jdialog没有专注于IE9

[英]Jdialog not getting focused in IE9

I am calling 2 jDialog back to back from a applet. 我正在从applet调用2 jDialog。 As soon as I select the option from the first dialog and click OK button, the applet window get focused and the second dialog box lost the focus. 一旦我从第一个对话框中选择了该选项并单击“确定”按钮,小程序窗口便被聚焦,第二个对话框失去了焦点。

The problem occurs only in the IEand works fine in firefox and chrome. 该问题仅在IE中出现,并且在Firefox和chrome中可以正常工作。 Please the code snippet. 请输入代码段。 (though the actual problem in my full code occurs only in IE9, i am not sure why this is not working in IE8 in the SSCCE) (尽管我的完整代码中的实际问题仅发生在IE9中,但我不确定为什么这在SSCCE的IE8中不起作用)

public class SampleApplet extends Applet{

protected JButton countryButton = new JButton("Select");

public synchronized void init()
{
    this.setBounds(new Rectangle(350,350));
    this.add(countryButton);


    countryButton.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent arg0) {
            getCountries();
            getCountries();             
        }

    });
}

protected void getCountries() {
    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    JComboBox CountriesCombo = new JComboBox();
    CountriesCombo.addItem("India");
    CountriesCombo.addItem("Japan");
    panel.add(CountriesCombo, gbc);

    JOptionPane     optionPane  = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);

    final JDialog   dialog      = optionPane.createDialog(panel, "Select Countries");
    dialog.setModal(true);

    dialog.addWindowListener ( new WindowAdapter ()
    {
        public void windowOpened ( WindowEvent e )
        {
            dialog.requestFocus ();
        }
    });
    dialog.pack();
    dialog.setLocationRelativeTo(null);
    dialog.setVisible(true);
}

} }

HTML Code: HTML代码:

<html>
<head>
    <title>Sample Code</title>
</head>
<body>
    <applet code="SampleApplet.class" width="350" height="350">
    </applet>

Can I get some help on this. 我可以在这方面寻求帮助吗?

It is possible that this behavior depends on browser you are running applet in (and seems you have proved it), so i'd offer you to try calling requestFocus() and requestFocusInWindow() on the dialog (or component inside it) to take the focus into the dialog box. 此行为可能取决于您在其中运行applet的浏览器(似乎已经证明了这一点),因此我建议您尝试在对话框(或对话框中的组件)上调用requestFocus()和requestFocusInWindow()以获取将焦点放到对话框中。

Once more - if you request focus BEFORE opening the dialog (before setVisible(true)) ofcourse it will fail since the dialog is not shown yet. 再说一次-如果您在打开对话框之前(在setVisible(true)之前)请求焦点,则它将失败,因为该对话框尚未显示。 If you call it AFTER the setVisible(true) method - if your dialog (or OptionPane) is modal - it will execute only when dialog is closed so it is meaningless there aswell. 如果在setVisible(true)方法之后调用它-如果您的对话框(或OptionPane)是模态的-它仅在对话框关闭时执行,因此在那里也没有意义。 You should add a WindowListener to your dialog and request focus into the dialog after it is opened. 您应该将WindowListener添加到对话框中,并在对话框打开后请求焦点。

Check this example: 检查以下示例:

public static void main ( String[] args )
{
    // Modal dialog
    final JDialog dialog = new JDialog (  );
    dialog.setModal ( true );

    // Adding listener
    dialog.addWindowListener ( new WindowAdapter ()
    {
        public void windowOpened ( WindowEvent e )
        {
            dialog.requestFocus ();
        }
    } );

    // Displaying dialog
    dialog.setVisible ( true );
}

Still, dialog.requestFocus () here is a platform-dependant call which might not focus/pop your dialog atop of other opened windows. 尽管如此,dialog.requestFocus()仍是一个依赖于平台的调用,它可能不会将对话框聚焦/弹出到其他打开的窗口之上。 In all Windows versions it should work fine. 在所有Windows版本中,它应该都能正常工作。

Also you can try using dialog.toFront () - this should pop the dialog and transfer focus into it. 您也可以尝试使用dialog.toFront()-这将弹出对话框并将焦点转移到其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM