简体   繁体   中英

Jdialog not getting focused in IE9

I am calling 2 jDialog back to back from a applet. 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. 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)

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>
<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.

Once more - if you request focus BEFORE opening the dialog (before setVisible(true)) ofcourse it will fail since the dialog is not shown yet. 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. You should add a WindowListener to your dialog and request focus into the dialog after it is opened.

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. In all Windows versions it should work fine.

Also you can try using dialog.toFront () - this should pop the dialog and transfer focus into it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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