JDK-4715272 : Focus problem when poping up a menu and selecting a JInternalFrame
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2002-07-16
  • Updated: 2004-12-22
  • Resolved: 2004-12-22
Related Reports
Relates :  
Description

Name: jk109818			Date: 07/15/2002


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

FULL OPERATING SYSTEM VERSION :
4NT  3.02B   Windows NT 5.00

A DESCRIPTION OF THE PROBLEM :
When switching between internal frames, using the right
mosue button and poping up a menu at the same time, focus
is not going to the popup menu.

This causes focus problems when using the arrow keys. Focus
is going to the invoker control, not the menu.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Compile and run the attached code
2. Right click on the second tree to get the popup menu
3. Press the down-arrow key.

EXPECTED VERSUS ACTUAL BEHAVIOR :
Actual:
The nodes on the tree view are getting selected.

Expected:
The items in the popup menu should be getting selected.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

import java.awt.*;
import java.awt.image.*;
import java.text.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;

public class Frame1 extends JFrame {

	JPanel contentPane;
	BorderLayout borderLayout1 = new BorderLayout();
	JDesktopPane desktop = new JDesktopPane();
	JPanel jPanel1 = new JPanel();
	JTextArea ta = new JTextArea();
	JInternalFrame jif1;
	JInternalFrame jif2;

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel
(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		Frame1 frame = new Frame1();
		frame.setLocation(100, 100);
		frame.setVisible(true);
	}

	public Frame1() {
		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		contentPane = (JPanel)getContentPane();
		contentPane.setLayout(borderLayout1);
		setSize(new Dimension(600, 300));
		setTitle("Focus Issue");
		contentPane.add(desktop);
		jif1 = new JInternalFrame("Frame1", true, true, true, true);
		jif1.setBounds(10, 10, 200, 100);
		JTree t1 = new JTree();
		final JTree t2 = new JTree();
		JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
true, t1, t2);
		jPanel1.add(sp);
		jif1.getRootPane().setFocusable(false);
		jif1.getContentPane().add(jPanel1, BorderLayout.NORTH);
		jif1.show();

		jif2 = new JInternalFrame("Frame2", true, true, true, true);
		jif2.setBounds(210, 10, 100, 100);
		jif2.getRootPane().setFocusable(false);
		jif2.getContentPane().add(ta, BorderLayout.NORTH);
		jif2.show();
		t2.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent e) {
				JPopupMenu pm = new JPopupMenu();
				pm.add("Hi");	pm.add("Yo");
				pm.show(t2, e.getX(), e.getY());
			}
		});

		setupFocusTimer();

		desktop.add(jif1);
		desktop.add(jif2);
		// switch focus to the second internal frame for this demo
		addWindowListener(new WindowAdapter() {
			public void windowOpened(WindowEvent e) {
				activateFrame(jif2);
				ta.requestFocusInWindow();
			}
		});
	}

	////////////////////////////
	////////////////////////////
	// helper functions
	////////////////////////////
	////////////////////////////


	static public void restoreFrame(JInternalFrame jif) {
		try {
			if (jif.isIcon()) {
				jif.setIcon(false);
			}
		}
		catch (java.beans.PropertyVetoException pve) {
			pve.printStackTrace();
		}
	}

	static public void selectFrame(JInternalFrame jif) {
		try {
			jif.setSelected(true);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	static public void activateFrame(final JInternalFrame jif) {
		jif.setVisible(true);
		restoreFrame(jif);
		jif.moveToFront();
		selectFrame(jif);
	}

	private void setupFocusTimer() {
		// show current focus item
		javax.swing.Timer timer = new javax.swing.Timer(50, new
ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FocusManager fm = FocusManager.getCurrentManager
();
				Component c = fm.getFocusOwner();
				JInternalFrame jif = desktop.getSelectedFrame();
				String s = "Focus = ";
				if (c == null) {
					s += "null";
				}
				else {
					String s2 = c.toString();
					s += s2.substring(0, s2.indexOf('['));
				}
				s += "; Frame = ";
				if (jif == null) {
					s += "null";
				}
				else {
					String s2 = jif.toString();
					s += s2.substring(0, s2.indexOf
("layout="));
				}
				setTitle(s);
			}
		});
		timer.start();
	}

	protected void processWindowEvent(WindowEvent e) {
		super.processWindowEvent(e);
		if (e.getID() == WindowEvent.WINDOW_CLOSING) {
			System.exit(0);
		}
	}
}

---------- END SOURCE ----------
(Review ID: 159380) 
======================================================================

Comments
EVALUATION Name: osR10079 Date: 07/22/2002 The problem is similar to 4710695 and 4715271. And so i reassign it to Swing. ###@###.### July 22, 2002 This problem needs more investigation. These focus issues will be tackled in the next release. ###@###.### 2002-11-05 Keyboard navigation in JPopupMenus is processed by changing focus to the invoker's root pane and installing key bindings in the root pane. This fails when the root pane is not focusable. This test case calls setFocusable(false) on the rootpanes of both JInternalFrames. There is no need for these calls and removing them completely solves the problem. Closing as not a bug. ###@###.### 2004-12-22 20:30:10 GMT
22-12-2004