JDK-4487103 : Focus issues with JInternalframe
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2001-08-01
  • Updated: 2005-04-13
  • Resolved: 2005-04-13
Related Reports
Duplicate :  
Description

Name: bsC130419			Date: 08/01/2001


java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)

Scenario 1:

Start app,
Create two windows (select file, new),
 and move them so that neither overlap (just for ease of use),
key text in each text box,
Double-click (or drag with mouse) to highlight the text,
Click on the Nop button of the active window (window1) so that focus is moved
to the button,
  From the window menu select the other window (window0), that is NOT active,
Click on the (original) window (window1) that is (now) NOT active,
The result of all this is...
The text is now selected and has focus - but it should be the button that has
focus.

Scenario 2:

Start app,
Create window0,
key some text in window0
create window1,
key some text in window1,
Select window0 from the window menu,
The result...
Nothing has focus and the tab key does not move focus to anywhere, but one
would expect the text to have focus.


/*
 * Author: aelco
 * Created: Thursday, July 05, 2001 3:11:46 PM
 * Modified: Thursday, July 05, 2001 3:11:46 PM
 */
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.beans.PropertyVetoException;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class Example extends JFrame {

	private JDesktopPane desktop = new JDesktopPane();
	private JMenuBar mnuBar = new JMenuBar();
	private JMenu mnuFile = new JMenu("File");
	private JMenu mnuEdit = new JMenu("Edit");
	private JMenu mnuWindow = new JMenu("Window");

	private int windowCount = 0;
		
	// fields used to position windows...
	private int cascadeX = 0;
	private int cascadeY = 0;
	
	public Example() {
		super("MDI Actions");

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {closeApp();}
		});

		mnuFile.add(new MyAction("New"));
		mnuFile.addSeparator();
		mnuFile.add(new MyAction("Exit"));

		mnuWindow.addSeparator();
		mnuWindow.add(new MyAction("Cascade"));

		mnuEdit.add(new MyAction("Cut"));
		mnuEdit.add(new MyAction("Copy"));
		mnuEdit.add(new MyAction("Paste"));
		mnuEdit.add(new MyAction("Delete"));

		mnuBar.add(mnuFile);
		mnuBar.add(mnuEdit);
		mnuBar.add(mnuWindow);
		setJMenuBar(mnuBar);

		setBounds(300,200,800,600);
		getContentPane().add(desktop, BorderLayout.CENTER);
		show();
	}

	private void newWindow() {
		String windowTitle = "Window " + windowCount++;
		AJInternalFrame newWindow = new AJInternalFrame(windowTitle);
		newWindow.addInternalFrameListener(new InternalFrameAdapter() {
			public void internalFrameClosed(InternalFrameEvent e)
				{removeWindow(e.getInternalFrame());}
			public void internalFrameActivated(InternalFrameEvent e)
			{/*System.out.println("Window activated")*/;}
		});
		JTextPane jtp = new JTextPane();
		newWindow.getContentPane().add(new JScrollPane(jtp),
BorderLayout.CENTER);
		newWindow.getContentPane().add(new JButton("Nop"),
BorderLayout.SOUTH);
		desktop.add(newWindow);
		newWindow.setBounds(cascadeX, cascadeY, 200, 200);
		newWindow.setVisible(true);
		cascadeX += 20;
		cascadeY += 20;
		Dimension dim = getSize();
		if(cascadeX > dim.width-200 || cascadeY > dim.height-200) {
			cascadeX = 0;
			cascadeY = 0;
		}

		mnuWindow.insert(new SelectAction(windowTitle, newWindow), 0);
	}

	private void removeWindow(JInternalFrame hWnd) {
		for(int x=0; x<mnuWindow.getMenuComponentCount(); x++) {
			Component c = mnuWindow.getMenuComponent(x);
			if(c instanceof JMenuItem) {
				Action act = ((JMenuItem)c).getAction();
				if(act instanceof SelectAction) {
					JInternalFrame mnuWnd = ((SelectAction)
act).getHWnd();
					if(mnuWnd == hWnd) {
						System.out.println("Remove
element " + x);
					}
				}
			}
		}
	}

	public void closeApp() {System.exit(0);}

	public static void main(String args[]) {
		Example myExample = new Example();
	}

	class MyAction extends AbstractAction {
		private String actionName;

		public MyAction(String name) {
			super(name);
			actionName = name;
		}

		public void actionPerformed(ActionEvent ae) {
			if(actionName.equals("Exit"))
				closeApp();
			else if(actionName.equals("New"))
				newWindow();
			else if(actionName.equals("Cut")) {
				AJInternalFrame hWnd = (AJInternalFrame)
desktop.getSelectedFrame();
				hWnd.toFront();
				try {hWnd.setSelected(true);}
				catch(PropertyVetoException e)
{System.out.println(e);}
				hWnd.cut();
			} else
				System.out.println("Unable to comlete request "
+ actionName);
		}
	}

	class SelectAction extends AbstractAction {
		JInternalFrame hWnd;
		public SelectAction(String name, JInternalFrame w) {
			super(name);
			hWnd = w;
		}
		public void actionPerformed(ActionEvent ae) {
			hWnd.toFront();
			try {hWnd.setSelected(true);}
			catch(PropertyVetoException e) {System.out.println(e);}
		}
		public JInternalFrame getHWnd() {return hWnd;}
	}
}

/*
 * Author: aelco
 * Created: Friday, July 06, 2001 9:55:19 AM
 * Modified: Friday, July 06, 2001 9:55:19 AM
 */
import java.awt.Component;
import javax.swing.JInternalFrame;

public class AJInternalFrame extends JInternalFrame {
	public AJInternalFrame(String windowTitle) {
		super(windowTitle, true, true, true, true);
	}
	public void cut() {
		System.out.println("Cut requested");
		//Find the active Component
		Component c = getFocusOwner();
		if(c == null) {
			System.out.println("No components found in
AJInternalFrame");
		}
		// JInternalFrame.getMostRecentFocusOwner() is new in Java SDK
1.4
		c = this.getMostRecentFocusOwner();
		if(c == null) {
			System.out.println("No recent focus owner in
AJInternalFrame either !!");
			return;
		}
		System.out.println(c.getClass());
	}
	public void copy() {}
	public void paste() {}
	public void delete() {}
}
(Review ID: 128871) 
======================================================================

Comments
EVALUATION Scenario 1 is a duplicate of 4302764 focus is not set in JInternalFrame. Scenario 2 is not reproducible in 1.6.0-ea-b31. Closing as duplicate of 4302764. ###@###.### 2005-04-13 01:05:49 GMT
13-04-2005