The simple testcase pasted below shows that Swing components that are already focused are giving up and re-claiming focus when they're clicked on. Running on the JDK shows that there should be no FOCUS_LOST or FOCUS_GAINED events fired at all. ***TESTCASE*** import java.awt.event.*; import javax.swing.*; public class Focus { public static void main(String args[]) { JButton b = new JButton(); b.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { System.out.println ("g"); } public void focusLost (FocusEvent e) { System.out.println ("l"); } }); JFrame f = new JFrame(); f.setSize(300,300); f.add(b); f.setVisible(true); } }
Created attachment 10371 [details] patch
Fixed, patch attached.
The improved test case shown below shows that this bug was fixed incorrectly. Since Component.processEvent and Component.processFocusEvent can be overridden we must handle this problem BEFORE these methods are invoked, as the test case below shows that they aren't called on the JDK (when you click on the button) but they ARE currently called in our implementation. ***TEST CASE*** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Focus { public static void main(String args[]) { JButton b = new JButton() { protected void processFocusEvent(FocusEvent e) { System.out.println ("processFocusEventCalled"); super.processFocusEvent(e); } protected void processEvent (AWTEvent e) { if (e instanceof FocusEvent) System.out.println ("processEvent called"); super.processEvent(e); } }; b.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { System.out.println ("\n\ngained"); Thread.dumpStack(); } public void focusLost (FocusEvent e) { System.out.println ("\n\nlost"); Thread.dumpStack(); } }); JFrame f = new JFrame(); f.setSize(300,300); f.add(b); f.setVisible(true); } }
Subject: Bug 25165 CVSROOT: /cvsroot/classpath Module name: classpath Branch: Changes by: Anthony Balkissoon <abalkiss@savannah.gnu.org> 05/11/30 19:19:24 Modified files: . : ChangeLog java/awt : Component.java Log message: 2005-11-30 Anthony Balkissoon <abalkiss@redhat.com> Fixes bug #25165 * java/awt/Component.java: (processFocusEvent): Don't check if focus opposite is the same as the receiving Component, this is now done in dispatchEventImpl. (dispatchEventImpl): Don't dispatch FocusEvents whose opposite Components are the same. CVSWeb URLs: http://savannah.gnu.org/cgi-bin/viewcvs/classpath/classpath/ChangeLog.diff?tr1=1.5753&tr2=1.5754&r1=text&r2=text http://savannah.gnu.org/cgi-bin/viewcvs/classpath/classpath/java/awt/Component.java.diff?tr1=1.90&tr2=1.91&r1=text&r2=text
Created attachment 10372 [details] new patch
This is fixed properly now.