-
Bug
-
Resolution: Fixed
-
P3
-
1.4.0
-
beta2
-
sparc
-
solaris_2.6
Name: ooR10001 Date: 12/19/2000
Method javax.swing.JComponent.isManagingFocus() has been declared as
deprecated due to CCC 4290675, and javadoc for this method has been removed.
But its behavior has been incompatible changed, the old applications
that use this method will fail without any essential reason.
This incompatibility should be fixed because the method deprecation
is not a cause of incompatible changes.
Here is an example:
---------------------------------------------
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.*;
class MyKListener implements KeyListener {
boolean eventOccured = false;
public MyKListener() {}
public synchronized void keyPressed(KeyEvent ke) {
eventOccured = true;
notifyAll();
}
public synchronized void keyReleased(KeyEvent ke) {
eventOccured = true;
notifyAll();
}
public synchronized void keyTyped(KeyEvent ke) {
eventOccured = true;
notifyAll();
}
public void resetOccur() {
eventOccured = false;
}
public synchronized void waitOccur() {
try {
wait(10000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
class MFocListener implements java.awt.event.FocusListener {
boolean eventOccured = false;
public synchronized void focusGained(java.awt.event.FocusEvent e) {
eventOccured = true;
notifyAll();
}
public void focusLost(java.awt.event.FocusEvent e) {
}
public void resetOccur() {
eventOccured = false;
}
public synchronized void waitEventOccured() {
if(!eventOccured) {
try {
wait(5000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
class MyPanel extends JPanel {
public MyPanel() {
super();
}
public boolean isManagingFocus() {
return true;
}
}
public class t {
public static void main(String[] args) {
final JFrame f = new JFrame();
f.setSize(100, 100);
final MyPanel p = new MyPanel();
MyKListener mkl = new MyKListener();
MFocListener flsnr = new MFocListener();
JButton but1 = new JButton("1");
JButton but2 = new JButton("2");
p.add(but1);
p.add(but2);
p.addKeyListener(mkl);
f.getContentPane().add(p);
f.addFocusListener(flsnr);
f.setVisible(true);
flsnr.resetOccur();
try {
SwingUtilities.invokeAndWait(
new Runnable() {
public void run() {
f.requestFocus();
}
}
);
} catch (InterruptedException e) {
} catch (java.lang.reflect.InvocationTargetException it) {
}
flsnr.waitEventOccured();
if (!flsnr.eventOccured) {
System.out.println("Window can not receive focus");
return;
}
mkl.resetOccur();
KeyEvent ke = new KeyEvent(p, KeyEvent.KEY_PRESSED,
System.currentTimeMillis(),
0, KeyEvent.VK_A);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ke);
if (!mkl.eventOccured) {
mkl.waitOccur();
}
if (!mkl.eventOccured) {
System.out.println("No keys dispatched to component's listeners");
f.dispose();
return;
}
f.dispose();
System.out.println("OKAY");
return;
}
}
---------------------------------------------
Output under jdk1.4:
------------------------
No keys dispatched to component's listeners
------------------------
Output under jdk1.3:
--------------------------
OKAY
--------------------------
Note, that this test example work correctly only if window gain focus manually.
======================================================================