Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Switching windows in Linux/Firefox loses keyboard focus. Workarounds?

843807May 29 2009 — edited Aug 22 2009
Hi,

I've been stumbling on an issue in which an applet gets into a state where it can receive mouse events but not keyboard events. This state occurs some of the time when switching from a non-modal dialog to the applet.

I've witnessed this behavior on:
Linux (fc8), Firefox 3.0.10, Java plug-in 1.6.0_13, Gnome 2.20.3
Sun Solaris (5.10), Firefox 3.0.8, Java plug-in 1.6.0_12, Sun Java Desktop System or CDE

I can not reproduce this behavior using appletviewer, nor can I reproduce it on the Mac (Opera/Firefox/Safari), nor on Windows (Firefox/IE).

I've crafted some code that shows the behavior:

FocusApplet.java:
import javax.swing.JApplet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.beans.*;

public class FocusApplet extends JApplet {

  JTextArea infoText;

    Object activeWindow;
    Object focusOwner;
    Object permanentFocusOwner;

    /**
       Applet contains two components.
       NORTH: Text field 
       CENTER: Info text
       The info text is updated whenever the following
       KeyboardFocusManager properties change:
          activeWindow
	  focusOwner
	  permanentFocusOwner
    **/

  public void init(){

      
      JTextField tf = new JTextField("Type here");

      infoText = new JTextArea();
      infoText.setEditable(false);
      infoText.setLineWrap(true);
      infoText.setWrapStyleWord(true);
      infoText.setBorder(new EtchedBorder());
      this.add(infoText, BorderLayout.CENTER);
      this.add(tf, BorderLayout.NORTH);
      
      KeyboardFocusManager focusManager =
	  KeyboardFocusManager.getCurrentKeyboardFocusManager();

      activeWindow=focusManager.getActiveWindow();
      permanentFocusOwner=focusManager.getPermanentFocusOwner() ;
      focusOwner=focusManager.getFocusOwner() ;
      updateText();

      focusManager.addPropertyChangeListener(
	  new PropertyChangeListener() {
	    public void propertyChange(PropertyChangeEvent e) {
		String prop = e.getPropertyName();
		if ("focusOwner".equals(prop)) {
		    focusOwner = e.getNewValue();
		    updateText();
		} else if ("permanentFocusOwner".equals(prop)) {
		    permanentFocusOwner = e.getNewValue();
		    updateText();
		    
		} else if ("activeWindow".equals(prop)) {
		    activeWindow = e.getNewValue();
		    updateText();
		}
	    }
      });

      // Create non-modal dialog
      JDialog jdl = new JDialog((Frame)null,"Extra dialog",
				false/*modal*/);
      jdl.setSize (300,550);
      jdl.setVisible(true);
  }

    public void updateText() {
	infoText.setText("Active window: "+getName(activeWindow)+
  	   "\nFocus owner: "+getName(focusOwner)+
	   "\nPermanent focus owner: "+getName(permanentFocusOwner));
    }

    public String getName(Object obj) {
	return (obj==null) ? "null" : obj.getClass().getName();
    }
}
Applet HTML:
<applet code="FocusApplet.class" width="400" height="400"></applet>
When I run this applet, I can click on the text field ("Type here") and enter text. Then, I switch between the empty dialog box and the applet using the window manager. (I.e., clicking on the dialog, then clicking on the applet.) Sometimes I see the following Keyboard Focus settings when I bring the applet to the front:

Active window: sun.plugin.viewer.frame.XNetscapeEmbeddedFrame
Focus owner: javax.swing.JTextField
Permanent focus owner: javax.swing.JTextField

In this case, clicking on the text field will allows the user to edit text. Good! However, 10%-50% of the time I get the following settings after I bring the applet to the front:

Active window: null
Focus owner: null
Permanent focus owner: javax.swing.JTextField

In this case, I can click on the applet, and I can highlight text in the text field, but I can not edit the text. (No carat appears. Bad!) Since there is no keyboard focus owner, the applet appears non-responsive.


I have a few questions:

1. Is this a Java plug-in bug? A Firefox bug? Who do I file a bug with, assuming there's not something I'm missing?
2. Can anyone suggest a workaround?

Thanks,

-David-
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 19 2009
Added on May 29 2009
1 comment
192 views