Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-2036991 | 1.4.0 | Devananda Jayaraman | P4 | Resolved | Fixed | beta |
Name: mt13159 Date: 09/13/2000
An applet running in the plug-in does not get focus when running under IE. You have to click in the applet to give it focus. The same applet running under Netscape gets the focus automatically. This is important for us because our products have login screens at the beginning, where the user must enter their user name and password. When the applet comes up, however, the login dialog does not have focus and the user cannot type anything.
A simple test case follows. It displays a simple lightweight component, which draws a circle. The circle will have a crosshair in it if it has focus. If you run this in the plug-in, on IE the circle comes up empty. You need to click the circle to give it focus. But on Netscape, the circle has focus automatically. This occurs in plug-in 1.1.3 and 1.3.
import java.awt.*;
import java.awt.event.*;
public class IEFocus extends Applet {
LightWeight lw = null;
public void init() {
// Create a lightweight component
lw = new LightWeight();
add(lw);
}
public void start() {
if (lw != null) {
lw.requestFocus();
}
}
private class LightWeight extends Component implements FocusListener, MouseListener {
boolean focused = false;
public LightWeight() {
addFocusListener(this);
addMouseListener(this);
}
public void paint(Graphics g) {
Dimension size = getSize();
int w = size.width;
int h = size.height;
g.setColor(getBackground());
g.fillRect(0, 0, w, h);
g.setColor(Color.black);
g.drawOval(0, 0, w-1, h-1);
if (focused) {
g.drawLine(w/2, 0, w/2, h);
g.drawLine(0, h/2, w, h/2);
}
}
public Dimension getPreferredSize() {
return new Dimension(150, 150);
}
public void focusGained(FocusEvent e) {
focused = true;
System.out.println("focusGained");
repaint();
}
public void focusLost(FocusEvent e) {
focused = false;
System.out.println("focusLost");
repaint();
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
this.requestFocus();
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
}
In Netscape, the PluginFrame is given focus at the following call stack:
PluginFrame.requestFocus()
PluginObject.setFocus()
JavaPluginObject::setFocus()
CJavaPluginView::SetWindow()
CNetscapePlugin::SetWindow()
NPP_SetWindow()
There is no corresponding code path in IE. A simple fix might be to add the following line to the end of CControlWindow::CreateJavaWindow():
m_pFrame->setFocus(TRUE);
This will make IE behave like Netscape.
Another related issue can be seen in the above test case. If you give the circle focus in Netscape, then click on another application, then click the Netscape title bar, the circle gets focus again. But in IE, if you do the same thing, the circle doesn't get focus back when you re-activate IE. This latter issue might be an IE problem, and might not be something the plugin can solve. But it's worth mentioning. These differences in behavior between IE and Netscape make it difficult to write code that works consistently across different browsers.
Note: There was similar bug reported(4218740) closed and the recommendation was to move requestFocus() in applet's init method to its start method, but even
doing that in above example this problem does not go away.
(Review ID: 109590)
======================================================================
Name: yyT116575 Date: 11/30/2000
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
I am not able to set initial focus for a component using the plug-in and IE5.
The following code demonstrates an attempt to set the initial focus to a
JComboBox. I can set initial focus using Netscape 4.7 and appletviewer, but I
cannot using IE5.
Note that a similar bug was reported as 4218740 and closed. The workaround
suggested in the previous bug report - ie. placing the call to requestFocus()
in the applet's start() method - does not work.
Here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* This demonstrates the behavior of an applet when attempting to
* set initial focus for a component. Attempts are made using
* ComponentListener events, the paint(), and start() events.
* This works in Netscape 4.7 and appletviewer, but fails in IE5.
*/
public class SimpleApplet extends JApplet
implements ComponentListener {
private JButton _button;
private JTextField _tf;
private JTextArea _ta;
private JCheckBox _checkBox;
public void init() {
System.out.println("In init()");
JPanel panel = createDisplay();
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.CENTER);
addComponentListener(this);
}
public void componentShown(ComponentEvent e) {
System.out.println("componentShown event from "
+ e.getComponent().getClass().getName());
setInitialFocus(_checkBox);
}
private void setInitialFocus(final JComponent comp) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("Requesting focus for " + comp);
// Calling Applet.requestFocus() allows any of its
// child components to requestFocus(), but it prevents
// any of the applet's text components from accepting
// input.
// SimpleApplet.this.requestFocus();
comp.requestFocus();
}
});
}
// this exhibits the same behavior as the
// componentShown method
public void paint(Graphics g) {
super.paint(g);
//setInitialFocus(_checkBox);
}
// this exhibits the same behavior as the
// componentShown method
public void start() {
System.out.println("In start()");
//setInitialFocus(_checkBox);
}
private JPanel createDisplay() {
JPanel display = new JPanel();
display.setLayout(new BoxLayout(display, BoxLayout.Y_AXIS));
display.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel tfPanel = new JPanel();
tfPanel.setLayout(new BoxLayout(tfPanel, BoxLayout.X_AXIS));
JLabel label = new JLabel("Label:");
_tf = new JTextField("JTextField");
_tf.setMaximumSize(new Dimension(200, 20));
_tf.setPreferredSize(new Dimension(200, 20));
_tf.setMinimumSize(new Dimension(200, 20));
_checkBox = new JCheckBox("JCheckBox");
tfPanel.add(label);
tfPanel.add(Box.createRigidArea(new Dimension(10, 0)));
tfPanel.add(_tf);
tfPanel.add(Box.createRigidArea(new Dimension(35, 0)));
tfPanel.add(_checkBox);
tfPanel.add(Box.createHorizontalGlue());
_ta = new JTextArea();
JScrollPane scroll = new JScrollPane(_ta);
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.X_AXIS));
_button = new JButton("JButton");
btnPanel.add(Box.createHorizontalGlue());
btnPanel.add(_button);
btnPanel.add(Box.createHorizontalGlue());
_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("In button click");
_checkBox.requestFocus();
}
});
// assemble the pieces:
display.add(tfPanel);
display.add(Box.createRigidArea(new Dimension(0, 15)));
display.add(scroll);
display.add(Box.createRigidArea(new Dimension(0, 15)));
display.add(btnPanel);
return display;
}
// - ComponentListener methods - //
public void componentHidden(ComponentEvent e) {
}
public void componentMoved(ComponentEvent e) {
}
public void componentResized(ComponentEvent e) {
}
}
(Review ID: 106286)
======================================================================
- backported by
-
JDK-2036991 Applet running in plug-in does not get focus in IE
- Resolved