JDK-4647551 : REGRESSION: KeyListener for JFrame doesn't work after click in JInternalFrame
  • Type: Bug
  • Status: Open
  • Resolution: Unresolved
  • Component: client-libs
  • Sub-Component: javax.swing
  • Priority: P4
  • Affected Version: 1.4.0
  • OS: windows_98
  • CPU: x86
  • Submit Date: 2002-03-05
  • Updated Date: 2008-11-19
Description

Name: jk109818			Date: 03/05/2002


FULL PRODUCT VERSION :
"java version "1.4.0-rc
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-rc-b91)
Java HotSpot(TM) Client VM (build 1.4.0-rc-b91, mixed mode)



FULL OPERATING SYSTEM VERSION :
Windows 98 [Version 4.10.2222]




A DESCRIPTION OF THE PROBLEM :
although the addKeyListener is written in the main frame
and also in the internalframe, it's not working after
internalframe got focus by the mouse, and not after it lost
focus again and the main frame got beck the focus.

REGRESSION.  Last worked in version 1.3.1

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.after the program is running I press Esc... it's working
for now
2.now I press on the internalframe with the mouse and try
to press the Esc again... not working

3.now I press on the main frame again with the mouse and
try to press the Esc again... not working


EXPECTED VERSUS ACTUAL BEHAVIOR :
I saw in the erlier versions that only 1 addKeyListener in
the main frame was enough to catch the keyEvent in the
program but now even after puting 2 listeners
1. in the main frame.
2. in the internal frame
it's not enough.

This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class mainDiamond extends JFrame
{
  	JDesktopPane desk;
  	Vector popups = new Vector();
	Dimension size;
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

	public mainDiamond()
	{
		super("Community");
    	mainDiamond mD 	   = this;
    	setSize(800, 600);
		size = getSize();
		screenSize.height = screenSize.height/2;
		screenSize.width = screenSize.width/2;
		size.height = size.height/2;
		size.width = size.width/2;
		int y = screenSize.height - size.height;
		int x = screenSize.width - size.width;
		setLocation(x, y);
    	this.addKeyListener(new KeyAdapter()
		{
			public void keyReleased(KeyEvent keyEvent)
			{
				int code = keyEvent.getKeyCode();
				if(code==27)
				try
				{
					System.out.println("ESC");
				}
				catch(Exception ioe){}
			}
		});
		desk = new JDesktopPane();
		getContentPane().add("Center", desk);
		intframe Rframe = new intframe(mD);
		AddInternalFrame(Rframe,1);
	}
	public void AddInternalFrame(JInternalFrame jif,int i)
	{
		popups.addElement(jif);
		desk.add(jif, new Integer(i));  // Keep sites on top for now
		try
		{
	    	jif.setSelected(true);
		}
		catch (java.beans.PropertyVetoException e2) {}
		jif.show();
	}
	public static void main(String args[])
	{
    	mainDiamond main = new mainDiamond();
		//main.diamond();
		main.setVisible(true);
  	}
}
class intframe extends JInternalFrame{

    public mainDiamond mD;
	public intframe pl;

	public intframe(mainDiamond MD) {
		setBounds(100,100,300,300);
		setClosable(true);
        setMaximizable(false);
        setIconifiable(false);
	    setResizable(false);
	    mD = MD;
		setTitle("Suppliers administrator");
        addInternalFrameListener(new MyInternalFrameListener());
		JDesktopPane desk = new JDesktopPane();
		desk.add(this,JDesktopPane.PALETTE_LAYER);
		this.addKeyListener(new KeyAdapter()
		{
			public void keyReleased(KeyEvent keyEvent)
			{
				int code = keyEvent.getKeyCode();
				if(code==27)
				try
				{
					System.out.println("ESC");
				}
				catch(Exception ioe){}
			}
		});
	}
    class MyInternalFrameListener extends InternalFrameAdapter
	{
    	public void internalFrameClosing(InternalFrameEvent event)
    	{
		}
    }

}
---------- END SOURCE ----------

Release Regression From : 1.3.1
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Review ID: 139023) 
======================================================================

Comments
EVALUATION Probably partially due to some focus issues in internal frames. Looking into cause. ###@###.### 2002-06-10 I investigated this a little more and my original guess was incorrect. Key presses will go to the focus owner. If the internal frame is selected key presses go to its content pane. You will want to add the key listener to there instead. Clicking the JFrame does not transfer focus. If you want focus to go back to the frame you will need to do that explicitly. Closing as not a bug. ###@###.### 2003-08-25 While it's true that clicking back on the main frame should not transfer focus, it's possible there's an event delivery regression to investigate. In the very least, we need to show developers how to acccomplish what the submitter wants. ###@###.### 2005-2-08 19:51:06 GMT
2005-02-08